└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # django-heroku 2 | Minimal configuration to host a Django project at Heroku 3 | 4 | ## Create the project directory 5 | * mkdir directory_name 6 | * cd directory_name 7 | 8 | ## Create and activate your virtuanenv 9 | * virtualenv -p python3 .vEnv 10 | * . .vEnv/bin/activate 11 | 12 | ## Installing django 13 | * pip install django 14 | 15 | ## Create the django project 16 | * django-admin startproject myproject 17 | 18 | ## Creating the Git repository 19 | * git init 20 | * Create a file called `.gitignore` with the following content: 21 | ``` 22 | # See the name for you IDE 23 | .idea 24 | # If you are using sqlite3 25 | *.sqlite3 26 | # Name of your virtuan env 27 | .vEnv 28 | *pyc 29 | ``` 30 | * git add . 31 | * git commit -m 'First commit' 32 | 33 | ## Hidding instance configuration 34 | * pip install python-decouple 35 | * create an .env file at the root path and insert the following variables 36 | - SECRET_KEY=Your$eCretKeyHere (Get this secrety key from the settings.py) 37 | - DEBUG=True 38 | 39 | ### Settings.py 40 | * from decouple import config 41 | * SECRET_KEY = config('SECRET_KEY') 42 | * DEBUG = config('DEBUG', default=False, cast=bool) 43 | 44 | ## Configuring the Data Base (You don't need that if you already had an database). 45 | * pip install dj-database-url 46 | 47 | ### Settings.py 48 | * from dj_database_url import parse as dburl 49 | 50 | default_dburl = 'sqlite:///' + os.path.join(BASE_DIR, 'db.sqlite3') 51 | 52 | DATABASES = { 53 | 'default': config('DATABASE_URL', default=default_dburl, cast=dburl), 54 | } 55 | 56 | 57 | ## Static files 58 | pip install dj-static 59 | 60 | ### wsgi 61 | * from dj_static import Cling 62 | * application = Cling(get_wsgi_application()) 63 | * Also don't forget to check "DJANGO_SETTINGS_MODULE". It is prone to frequent mistakes. 64 | 65 | ### Settings.py 66 | * STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') 67 | 68 | ## Create a requirements-dev.txt 69 | pip freeze > requirements-dev.txt 70 | 71 | ## Create a file requirements.txt file and include reference to previows file and add two more requirements 72 | * -r requirements-dev.txt 73 | * gunicorn 74 | * psycopg2 75 | 76 | ## Create a file Procfile and add the following code 77 | * web: gunicorn project.wsgi 78 | * You can check in django website or heroku website for more information: 79 | https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/gunicorn/ 80 | https://devcenter.heroku.com/articles/django-app-configuration 81 | 82 | ## Create a file runtime.txt and add the following core 83 | * python-3.6.0 (You can currently use "python-3.7.3") 84 | 85 | ## Creating the app at Heroku 86 | You should install heroku CLI tools in your computer previously ( See http://bit.ly/2jCgJYW ) 87 | * heroku apps:create app-name (you can create by heroku it's self if you wanted.) 88 | You can also login in heroku by: heroku login 89 | Remember to grab the address of the app in this point 90 | 91 | ## Setting the allowed hosts 92 | * include your address at the ALLOWED_HOSTS directives in settings.py - Just the domain, make sure that you will take the protocol and slashes from the string 93 | 94 | ## Heroku install config plugin 95 | * heroku plugins:install heroku-config 96 | 97 | ### Sending configs from .env to Heroku ( You have to be inside tha folther where .env files is) 98 | * heroku plugins:install heroku-config 99 | * heroku config:push -a 100 | 101 | ### To show heroku configs do 102 | * heroku config 103 | (check this, if you fail changing by code, try changing by heroku dashboard) 104 | 105 | ## Publishing the app 106 | * git add . 107 | * git commit -m 'Configuring the app' 108 | * git push heroku master --force (you don't need "--force") 109 | 110 | ## Creating the data base (if you are using your own data base you don't need it, if was migrated there) 111 | * heroku run python3 manage.py migrate 112 | 113 | ## Creating the Django admin user 114 | * heroku run python3 manage.py createsuperuser (the same as above) 115 | 116 | ## EXTRAS 117 | ### You may need to disable the collectstatic 118 | * heroku config:set DISABLE_COLLECTSTATIC=1 119 | 120 | ### Also recommend set this configuration to your heroku settings 121 | * WEB_CONCURRENCY = 3 122 | 123 | ### Changing a specific configuration 124 | * heroku config:set DEBUG=True 125 | --------------------------------------------------------------------------------