Record as a reminder when running the Django app on Heroku. Part 1 is about displaying the Django app's start page (It Work page) on Heroku.
Next time Deploy the Django app to Heroku [Part 2] ** My environment **
Heroku account Reference: Heroku beginners try Hello, Heroku
Install virtualenv (installed in python3 environment) Reference: Manage python environment with virtualenv
The application created this time has the following configuration.
Constitution
myProject
├── venv
├── Procfile
├── db.sqlite3
├── manage.py
├── myDjango
│ ├──__pycache__
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── requirements.txt
└── runtime.txt
Root directory name: myProject
Django project name: myDjango
** Create / move project directory **
$ mkdir myProject
$ cd myProject
** Building and starting a virtual environment **
$ virtualenv venv
$ source venv/bin/activate
Build a virtual environment named venv with virtualenv venv
.
Run the virtual environment venv with source venv / bin / activate
. (Venv)
will now appear before the terminal username.
(Supplement)
When you want to end the execution of the virtual environment, execute deactivate
in the terminal.
** Install django-toolbelt **
$ pip install django-toolbelt
By installing django-toolbelt, the following packages will be installed.
** Create a Django project **
$ django-admin.py startproject myDjango ./
Create a Django project called myDjango
in your current directory.
** Change language / time zone **
Rewrite the following part of settings.py
in the myDjango directory.
myDjango/settings.py
LANGUAGE_CODE = 'ja'
TIME_ZONE = 'Asia/Tokyo'
** Migrate **
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying sessions.0001_initial... OK
OK if it looks like this To put it simply, migration is a feature that allows you to create a table in a database in Python without writing SQL. Reference: ["Migration" that Rails beginners can easily trip over](https://www.transnet.ne.jp/2015/12/29/rails "Migration" that Rails beginners can easily trip over "colnr /" https://www.transnet. ne.jp/2015/12/29/rails "Migration" colnr / ") that beginners can easily trip over
** Start server **
$ python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
July 16, 2017 - 02:53:26
Django version 1.11.3, using settings 'myDjango.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Execute python manage.py runserver
and when the above display is displayed, it's OK
By executing this command, you can see the app at http://127.0.0.1:8000/
.
Press Ctrl + C
to exit.
** Check if the app starts locally ** Access http://127.0.0.1:8000/ from your browser. If all goes well, you'll see a page like the one in the image below.
** Creating .gitignore ** Create a .gitignore file and write the following:
.gitignore
*.pyc
venv
staticfiles
db.sqlite3
commit
$ git init
$ git add .
$ git commit -m "I created a Django app"
** Creating a Procfile **
$ echo "web: gunicorn myDjango.wsgi --log-file -" > Procfile
Procfile is a file that teaches Heroku what to do. Reference: Role of Heroku's Procfile
** Creating runtime.txt **
$ echo "python-3.6.1" > runtime.txt
Specify the Python version in runtime.txt.
** Creating requirements.txt **
$ pip freeze > requirements.txt
You can create a file like this
requirements.txt
dj-database-url==0.4.2
dj-static==0.0.6
Django==1.11.3
django-toolbelt==0.0.1
gunicorn==19.7.1
psycopg2==2.7.1
pytz==2017.2
static3==0.7.0
** Make the Django app available on Heroku ** Add the following to the last line of settings.py.
myDjango/settings.py
# Parse database configuration from $DATABASE_URL
import dj_database_url
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Allow all host headers
ALLOWED_HOSTS = ['*']
# Static asset configuration
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
Rewrite wsgi.py
myDjango/wsgi.py
import os
from dj_static import Cling
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myDjango.settings")
application = Cling(get_wsgi_application())
** Creating a Heroku app **
$ heroku create
** Setting Heroku environment variables **
$ heroku config:set DISABLE_COLLECTSTATIC=1
If you do not do this, you will get an error when pushing.
** Push to Heroku **
$ git add .
$ git commit -m "Made it work on Heroku"
$ git push heroku master
Push successful when remote: Verifying deploy ... done.
is displayed
** Migrate on Heroku **
$ heroku run python manage.py migrate
If you run heroku run 〇〇
, you can execute the command 〇〇 on heroku.
** Check if it works on Heroku **
$ heroku open
You can access the application on Heroku created this time with heroku open
.
If all goes well, you'll see a page like the one in the image below.
This time, I even ran a Django-made app on Heroku. Next time, we will create a model for the management site.
Next time Deploy the Django app to Heroku [Part 2]
Recommended Posts