[PYTHON] Publish django project developed in Cloud9 on heroku

Publish django project developed in Cloud9 on heroku

prologue

I tried using heroku a little in previous article. Three months after that, R's experience points have increased, but this time I'm talking about using Python instead of R.

There are many packages for R, a lot of books were released last year, and recently there is slack where all the R enthusiasts I met through TokyoR participate all the time, so it is very convenient, but basically Because it was created with statistical processing in mind, awk was used for data preprocessing. However, awk is troublesome to use in the company's Windows 7 (32bit) environment. So, in fact, I started collecting information around the summer of 2015 and participated in various Python-related study sessions (and julia).

-Everyone's Python Study Group StartPythonClub (#StaPy) -Python Mokumokukai @ Allied Architects (#mokupy) -Python Beginners' Gathering (#PyNyumon)

It seems that I participated a lot when I put them side by side, but I participated multiple times only in the top StartPython Club, and the others have only participated once.

As a Python beginner, I received a consultation from a colleague at work saying "I want to make a web application with django".

  1. With Python 3
  2. With django 1.9
  3. Create a web app on Cloud9 IDE
  4. Migrate from SQLite3 to PostgreSQL
  5. Deploy to heroku

It is a story that I did my best.

I had a hard time especially in 3-5, so I would like to focus on that area.

Things to prepare

--Cloud9 account: If you don't have one, sign up from here --heroku account: If you don't have one, sign up here [https://signup.heroku.com/login) --Computers that can log in to Cloud9 and heroku ――The concept of the app you want to make

Only this. By the way, the reason for Cloud9 is that the integrated development environment on the cloud, which allows you to prepare the same environment, was the most convenient for answering various questions of colleagues who started to be interested in Python.

Also, because I have a Mac at home and Windows 7 at work, I don't want to be swayed by the differences in OS, character code, and line feed code.

Python3 & django app development on Cloud9

Create a django workspace

Log in to Cloud9 and click Create a new workspace from the dashboard screen. ワークスペースの追加

Enter the project name (tentatively abc), select the django template from the template list at the bottom, and click the Create workspace button. djangoプロジェクトを選択

A workspace has been created with the django template. The initial folder structure is like this. 初期のフォルダ構成 c9_abc is the django project folder.

Make it a Python3 environment

The Cloud9 template configures Python 2.7 and the corresponding django. I want to develop in Python3, so let's change the settings so that it works in Python3.

Open the project settings screen with Cloud9 --Preferenes from the workspace menu. Preferences

Change Python 2 to Python 3 in the Language Support section. Language Support

However, even if Preference is Python3, if you set python --version on the command line, it will still be Python2.7.

$ python --version
Python 2.7.6

I plan to hit python manage.py migrate etc. many times later, so make sure that / usr / bin / python points to Python3.

$ sudo mv /usr/bin/python /usr/bin/python2
$ sudo ln -s /usr/bin/python3 /usr/bin/python
$ python —version
python 3.4.3    

I referred to this article for the method. [^ 1] Cloud9 is also convenient because you can do this without hesitation.

Now let's run the abc project. Click Run Project on the menu bar. Run Project

That doesn't work. djangoエラー

It seems to say "I don't have django". Maybe django isn't included because of Python3. So, install django. Since it is Python3, let's use pip3.

$ sudo pip3 install django

When I clicked Run Project again, it worked fine this time. django動いた

Replace with a set of django files for heroku

If you just develop on Cloud9, you can leave it as it is, but in order to deploy it to heroku later, replace it with the django project template prepared by heroku. I referred to this article [^ 2] for the procedure around here.

First, delete the django project that was automatically configured when you created your workspace. Not only the project folder but also manage.py etc. will be deleted. さっくり削除

Then bring the django template for heroku from GitHub.

$ django-admin.py startproject --template=https://github.com/heroku/heroku-django-template/archive/master.zip --name=Procfile c9_abc

The last c9_abc has the same name as the django project folder you deleted earlier.

Looking at the folder structure, it's a little strange. The hierarchy is off by one. 階層がひとつずれている

Move up one level at a time so that manage.py is at the top level. The abc \ c9_abc \ c9_abc folder remaining at the bottom layer will be empty, so delete it. 階層を修正

In the article I referred to, I edited settings.py and wsgi.py so that they work on Cloud9, but when deploying to heroku, it is troublesome to return them again, so I will install the necessary libraries.

$ sudo pip3 install dj-database-url
$ sudo pip3 install Whitenoise
$ sudo pip3 install Gunicorn

Check the operation with Run Project.

App development & testing

I will omit the contents of the app this time.

After all, I'm new to Python, so I have to check the Python grammar and how to use the library before django to proceed.

For how to use the Python library, see [Python Library Carefully Selected Recipe (Technical Review)](http://www.amazon.co.jp/Python-%E3%83%A9%E3%82%A4%E3%83% 96% E3% 83% A9% E3% 83% AA% E5% 8E% B3% E9% 81% B8% E3% 83% AC% E3% 82% B7% E3% 83% 94-% E6% B1% A0 % E5% 86% 85-% E5% AD% 9D% E5% 95% 93 / dp / 4774177075 / ref = sr_1_1? Ie = UTF8 & qid = 1457793060 & sr = 8-1 & keywords = python +% E3% 83% A9% E3% 82% Thank you very much for your help (A4% E3% 83% 96% E3% 83% A9% E3% 83% AA).

For django, I'm especially grateful to the following sites.

Although I was sick, I managed to make something that worked, and I showed it to my colleagues at work and corrected it. With Cloud9, unlike local development, you can show the app just by sending the URL, or you can share the same workspace if you have a colleague with a Cloud9 account.

As an aside, explaining to my colleagues about the app I created this time has greatly improved my understanding of django.

Migrating from SQLite3 to PostgreSQL

Well, this is a completed app, but there is one more thing to do before deploying it on heroku. By default django uses SQLite3. If you deploy it to heroku as it is, it will work for the time being, but there is such a description in the help of heroku.

SQLite on Heroku

In short, it seems that "it works for the time being, but it will disappear in up to 24 hours due to the mechanism". By the way, I tried to see how long it would disappear, but it didn't disappear even after about a week. I don't know if it happened or because I check every day "Is it gone?", But I don't want to be so excited in production operation, so I decided to move to PostgreSQL obediently. Here, I referred to this article [^ 3] and this article [^ 4].

Since PostgreSQL is originally installed in the Cloud9 workspace, there is no need to install it anew, but if you look at the status, it is down, so start the service.

$ psql --version
psql (PostgreSQL) 9.3.11
$ sudo service postgresql status
9.3/main (port 5432): down
$ sudo service postgresql start
 * Starting PostgreSQL 9.3 database server
    ...done.

Install the psycopg2 library for using PostgreSQL from Python.

$ sudo pip3 install psycopg2

Then log in to PostgreSQL. Please note that there are two sudos.

$ sudo sudo -u postgres psql

Create a login user (myuser in this case) in the PostgreSQL console.

postgres=# CREATE ROLE myuser LOGIN;
CREATE ROLE
postgres=# \password myuser
Enter new password: 
Enter it again: 

Create a database (abc in this case) to be used by the app. In Cloud9, I got angry with new encoding (UTF8) is incompatible with the encoding of the template database (SQL_ASCII) without adding the last TEMPLATE template0, but I was worried for a while because I didn't know what to do.

postgres=# CREATE DATABASE abc OWNER myuser ENCODING 'UTF8' TEMPLATE template0;                                                                                
CREATE DATABASE
postgres=# \q

Now the PostgreSQL side is ready. Then edit settings.py in the django project (c9_abc) folder.

#DATABASES = {
#    'default': {
#        'ENGINE': 'django.db.backends.sqlite3',
#        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
#    }
#}   
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', 
        'NAME': 'abc',
                # The following settings are not used with sqlite3:
                'USER': ’myuser',
                'PASSWORD': 'your_password_here',
                'HOST': '127.0.0.1', 
                'PORT': 5432, 
            }
}

The last step was a little addictive. In the articles I referred to, both are written as OK with python manage.py syncdb, but the release note of django 1.9 says The syncdb command is removed., which is a function that can no longer be used. I will. When I looked it up, it was described in official django 1.8 documentation, where syncdb is migrate. It's an alias, and the difference seems to be that it includes a step to create a superuser. That means

$ python manage.py migrate
$ python manage.py createsuperuser

That's OK.

The migration should now be complete. Just in case, let's check if the app works properly.

Deploy to heroku

Since it is a template originally provided by heroku, files (runtime.txt and requirements.txt) that tell heroku the Python version and libraries to install are created in the root folder. However, heroku is also Python 2.7 by default, so modify runtime.txt.

[runtime.txt]
python-3.4.3

Then update requirements.txt as well.

$ pip3 freeze > requirements.txt

However, if nothing is done, all the libraries included in the Cloud9 Python3 environment will be included, so check the contents and delete unnecessary library lines. If the environment is turned off with venv etc. first, there is no need to delete unnecessary library lines. There are library dependencies, so if you want to do it properly, turn off the environment first.

Then install the heroku toolkit.

$ wget -O- https://toolbelt.heroku.com/install-ubuntu.sh | sh

Once the installation is complete, log in to heroku.

$ heroku login

If you haven't created a git repository for your app development work, create one.

$ git init
$ git add -A
$ git commit -m 'initial commit'

Create a repository for your app on heroku. In the example below, the app name is myherokuapp. Even if you don't add this, heroku will assign it automatically, but it will be a long name of about 20 characters.

$ heroku create myherokuapp

Then it's time to deploy.

$ git push heroku master
Counting objects: 130, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (121/121), done.
Writing objects: 100% (130/130), 1.91 MiB | 726.00 KiB/s, done.
Total 130 (delta 52), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Installing python-3.4.3
remote:      $ pip install -r requirements.txt
remote:        Collecting Django==1.9.4 (from -r requirements.txt (line 1))
remote:          Downloading Django-1.9.4-py2.py3-none-any.whl (6.6MB)

(Omitted) remote: https://myherokuapp.herokuapp.com/ deployed to Heroku remote: remote: Verifying deploy.... done. To https://git.heroku.com/myherokuapp.git * [new branch] master -> master

After waiting for a while, the deployment will be completed as above. If you get an error, check the log and error message, and make any necessary corrections.

Let's take a break. Initialize the database and create superuser on heroku.

$ heroku run python manage.py migrate
$ heroku run python manage.py createsuperuser

Let's open the url of the app displayed during the deployment (https://myherokuapp.herokuapp.com/ in the above example).

Did it work without problems? Then turn off django's debug mode. Edit settings.py on Cloud9.

# DEBUG=True
DEBUG=False

Reflect this fix on heroku.

$ git add .
$ git commit -m 'Debug mode off'
$ git push heroku master

Keep in mind that if you fix it in the future, you'll be using these three lines (although you'll change the commit comment as appropriate).

That's it. If you have any misunderstandings or mistakes, please do not hesitate to let us know by editing request. Thank you.


This article was written by @hirokiky's PileMd

Reference article

Recommended Posts

Publish django project developed in Cloud9 on heroku
Publish DJango page on heroku: Practice
Deploy django project to heroku
Register your Django application in your project
Deploy your Django application on Heroku
python + django + scikit-learn + mecab (1) on heroku
python + django + scikit-learn + mecab (2) on heroku
Django blog on heroku: login implementation
How to deploy a Django app on heroku in just 5 minutes
Deploy Django api on heroku (personal note)
Notes on creating static files in Django
Deploy the Django app on Heroku [Part 2]
Deploy the Django app on Heroku [Part 1]
[Django] css in the project cannot be read
Django Heroku Deploy 1
Redis on Heroku
Models in Django
shimehari on heroku
Django Project Baseline
Django Heroku Deploy 2
Forms in Django
How to deploy a Django application on Alibaba Cloud
Publish your Django app on Amazon Linux + Apache + mod_wsgi
Memo of deploying Django × Postgresql on Docker to Heroku
Django page released on heroku: Preparation my addictive point
Miscellaneous notes about deploying the django app on Heroku
How to reference static files in a Django project