Due to various errands, I decided to deploy the locally made Django app to Heroku, but it took a long time, so as a memorandum.
(I think you know it when you read this article ...) Django seems to be a web framework that runs on Python2 and 3 ... It's "likely" because I'm not familiar with web frameworks. If you are interested, please refer to the official document ...?
Reference: Django documentation
Moving what was made in the local environment to the actual production environment ... Maybe. Web apps like Django weren't as easy as just uploading a website ... Below, I'll spell out the general flow of this deployment.
If you haven't mastered Django yet, there was a nice article here, so learn from here ...! Explanation of Python Web application (Django) in an easy-to-understand manner even for beginners Master Django fastest
--General network environment (because you upload files) --Django app completed (using sqlite3 database) (virtualenv used) --CMD (command prompt) can be used --git installed --Heroku account owned
The author who made the first web application using Django. It's slimy, it's moving, it's DB, it's moving One day, one day, it's finally time to move it online ... (story style)
DjangoApp/ ├ env/ (virtualenv) ├ manage.py ├ db.sqlite3 ├ Django App / (the one from the beginning) │ ├ wsgi.py │ └ setting.py └ App / (application body)
When I investigated what was necessary when deploying, "I put db.sqlite3 (sqlite database body), * .pyc, and env / and below in .gitignore". I made .gitignore obediently, deleted the cache with "git rm -r --cached." And committed
Reference: Reflect the settings of [Qiita] .gitignore
DjangoApp/ ├ env/ ├ manage.py ├ db.sqlite3 ├ DjangoApp/ │ ├ wsgi.py │ └ setting.py ├ App/ └ .gitignore (New)
When uploading a file to Heroku, it seems that three kinds of sacred treasures "Procfile (without extension)" "requirements.txt" "runtime.txt" are required, so I will start creating
Procfile It tells Heroku that "This app should be launched like this! ☆" For the time being, I decided to write it exactly as it was written by various people.
Procfile
web: gunicorn DjangoApp.wsgi --log-file -
The Django App part describes your application name. And it seems that I have to install this "gunicorn" and so on in virtualenv later ... Mmm, it takes a lot of time and effort ...
requirements.txt You need to contact Heroku with the modules needed to run this application. Requirements.txt is used for that. (maybe) This is easy. After starting virtualenv with the command
CMD
pip freeze > requirements.txt
And it is sufficient. You should have a file like the one below.
requirements.txt
Django==1.11.1
virtualenv==15.1.0
runtime.txt It seems to tell Heroku the version of python you are using now ... (I'm not sure)
CMD
python-2.7.x
The 2.7.x part is the version of Python installed on your computer.
DjangoApp/ ├ env/ ├ manage.py ├ db.sqlite3 ├ DjangoApp/ │ ├ wsgi.py │ └ setting.py ├ App/ ├ requirements.txt (New) ├ runtime.txt (New) ├ Procfile (New) └ .gitignore
After investigating, it seems that various additional modules are required for deployment ... The above gunicorn, djando-static for handling static files, dj_database_url for handling databases, etc ... I think it is troublesome to handle each one. When I was there, I found an ally who could put these together! It's django-toolbelt ...!
CMD
pip install django-toolbelt
Install toolbelt as. All the necessary modules are available ...!
...... If you are a smart person, you should notice "this guy (laughs)" ... Yes, the module installed by toolbelt at this time is not written in requirements.txt ... I did not notice this for more than half a day Also spent time ... Correctly, let's update pip freeze again at this timing and update requirements.txt! !!
requirements.txt
dj-database-url==0.4.2
dj-static==0.0.6
Django==1.11.1
django-toolbelt==0.0.1
gunicorn==19.7.1
psycopg2==2.7.3
pytz==2017.2
static3==0.7.0
virtualenv==15.1.0
From what I hear, it seems that the sqlite3 database cannot be used on Heroku (I wonder if it can be installed ...?) Therefore, it is necessary to use (?) Postgres that is installed as standard on Heroku ...
…… And the dj-database-url for that! Open setting.py and find the following part.
setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
Rewrite the description about this DB as follows
setting.py
if "COMPUTER-NAME" in hostname:
#Debug environment
# DEBUG = True //I also switched the debug mode here
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
ALLOWED_HOSTS = [] #I'm not sure, but this seems to be important too
else:
#Production environment
# DEBUG = False
import dj_database_url
db_from_env = dj_database_url.config()
DATABASES = {
'default': dj_database_url.config()
}
ALLOWED_HOSTS = ['*']
This will use sqlite3 in your local environment and Postgres in your Heroku environment. (Likely) COMPUTER-NAME is the name of your device. (I saw that I should write local, but it didn't work in my environment ...) Also, if you (almost certainly) get angry with "I'm afraid you don't have a hostname!", Add the following to the beginning of setting.py.
setting.py
.
from socket import gethostname
hostname = gethostname()
.
It's time to touch wsgi.py, which I haven't touched in the local environment ...! I think it's okay just to play with it a little like the following
setting.py
import os
from dj_static import Cling
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "DjangoApp.settings")
application = Cling(get_wsgi_application())
Django App has its own project name. …… After all, what was wsgi.py (Sorega Karanai)
DjangoApp/ ├ env/ ├ manage.py ├ db.sqlite3 ├ DjangoApp/ │ ├ wsgi.py (modified) │ └ setting.py (modified) ├ App/ ├ requirements.txt ├ runtime.txt ├ Procfile └ .gitignore
Now that we're ready, launch CMD and head to Heroku!
CMD
git add .
git commit -am "Launching now!"
heroku login
> Enter your Heroku credentials.
> Email: [email protected]
> Password (hidden):
> Logged in as [email protected]
heroku create NAME
> https://NAME.herokuapp.com/ | https://git.heroku.com/NAME.git
git push heroku master
>Something long
heroku run python manage.py migrate
>The usual migrate command flows
heroku open
The final Heroku open will open the application for you. (In my case, it was the first Application Error ...) That's it! The rest is operation! !! Thank you for your hard work! !! !! (tired
It's hard to write an impression about this, but it was so difficult for beginners to think, "What a detour ... Can't you make it smarter?" ... (´; ω; `) In particular, there are few references for the latest (ver1.11) ... After all, I was reading the official English document (laughs). Spread, Django's circle!
Deployed to Heroku safely ... I thought, but the management site ...
It's broken ... (´ ・ ω ・ `) For some reason, it doesn't read CSS ... I added the following to setting.py to handle static files, but ... no ...!
setting.py
STATIC_URL = '/static/'
# Static asset configuration
STATIC_ROOT = 'staticfiles'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
...... Well, I will fix it patiently!