[PYTHON] Deploy the Django app on Heroku [Part 2]

Introduction

Record as a reminder when running the Django app on Heroku. In Part 2, you'll be able to use the Django app's admin page to create and manipulate models.

Last time Deploy Django app to Heroku [Part 1]

What I did last time

I've even reached the point where the Django app's start page (It Work page) is displayed on Heroku.

What to do this time

** My environment **

macOS Sierra 10.12.5 Python 3.6.1 virtualenv 15.1.0

Configuration of the application to be made this time

Constitution


myProject
├── Procfile
├── blog
│   ├── __init__.py
│   ├── __pycache__
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── db.sqlite3
├── manage.py
├── myDjango
│   ├── __init__.py
│   ├── __pycache__
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── requirements.txt
└── runtime.txt

procedure

This time, we will actually create an app Blog that adds the title, content, and date.

Working locally

Create a new app

** Create a blog app **

$ python manage.py startapp blog

** Enable the blog app ** Added blog to ʻINSTALLEF_APPS in myDjango / settings.py`

myDjango/settings.py


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',
]

Create a new model

** Create a model to use in the blog app ** Add the following to blog / models.py

blog/models.py


class Contents(models.Model):
    objects = None
    title = models.CharField(max_length=20)
    word_text = models.CharField(max_length=140)
    date_time = models.DateTimeField('Posted date')

** Create migration file **

$ python manage.py makemigrations blog 
Migrations for 'blog':
  blog/migrations/0001_initial.py
    - Create model Contents

** Migrate **

$ python manage.py migrate 
Operations to perform:
  Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
  Applying blog.0001_initial... OK

Allow the management page to handle the model

Rewrite blog / admin.py as follows.

blog/admin.py


from django.contrib import admin
from .models import Contents


class ContentsAdmin(admin.ModelAdmin):
    list_display = ('id', 'title', 'word_text', 'date_time')


admin.site.register(Contents, ContentsAdmin)

Create an administrator

$ python manage.py createsuperuser
Username (leave blank to use 'If you do not enter anything, this part will be the user name'): <Username at login>
Email address: <mail address>
Password: <password>
Password (again): <Re-enter password>
Superuser created successfully.

** Start server **

$ python manage.py runserver

Verification

Go to http://127.0.0.1:8000/admin

管理ページログイン画面

If you enter the user name and password and the screen below appears, it is successful locally. 管理ページ

Run on Heroku

push

$ git add .
$ git commit -m "I created a blog app"
$ git push heroku master

Migrate

Migration files are not created on Heroku (managed by git). However, since the database handled by Heroku is postgres, migration on Heroku (applying the contents of the model to the database) is required.

$ heroku run python manage.py migrate 

Creating an administrator

An administrator created locally is registered in the local database (SQLite3), so you need to create an administrator on Heroku.

$ heroku run python manage.py createsuperuser

Verification

** Open the app on Heroku **

$ heroku open

** Display the management page ** Open by adding / admin to the URL of the opened page Example) https://heroku app name.herokuapp.com/admin Then, the Login page as confirmed earlier will appear, so log in.

** Display the Contents model management page of the Blog app ** Click Site Administration-> BLOG-> Contents-> Add. Heroku上の管理ページ.png

** Add content ** The fields will be displayed as set in [Contents class](#Create new model), so enter them appropriately and click "Save". スクリーンショット 2017-07-16 4.26.12.png

** Check on the model management screen ** OK if the content you added earlier is displayed スクリーンショット 2017-07-16 4.26.58.png

bonus

About DB handled by Django and DB handled by Heroku

Django uses SQLite by default and is easy to use. However, since Heroku basically cannot handle SQLite and postgreSQL is the standard, Part 1 Is set to handle postgreSQL on Heroku.

Take a look at the database on Heroku.

To access the database on Heroku, run the following command.

$ heroku pg:psql

If you can access it, App name :: DATABASE => will be displayed, so enter \ z. Note) Display the table list in \ z: postgres

dry-brook-87010::DATABASE=> \z
                                            Access privileges
 Schema |               Name                |   Type   | Access privileges | Column privileges | Policies
--------+-----------------------------------+----------+-------------------+-------------------+----------
 public | auth_group                        | table    |                   |                   |
 public | auth_group_id_seq                 | sequence |                   |                   |
 public | auth_group_permissions            | table    |                   |                   |
 public | auth_group_permissions_id_seq     | sequence |                   |                   |
 public | auth_permission                   | table    |                   |                   |
 public | auth_permission_id_seq            | sequence |                   |                   |
 public | auth_user                         | table    |                   |                   |
 public | auth_user_groups                  | table    |                   |                   |
 public | auth_user_groups_id_seq           | sequence |                   |                   |
 public | auth_user_id_seq                  | sequence |                   |                   |
 public | auth_user_user_permissions        | table    |                   |                   |
 public | auth_user_user_permissions_id_seq | sequence |                   |                   |
 public | blog_contents                  | table    |                   |                   |
 public | blog_contents_id_seq           | sequence |                   |                   |
 public | django_admin_log                  | table    |                   |                   |
 public | django_admin_log_id_seq           | sequence |                   |                   |
 public | django_content_type               | table    |                   |                   |
 public | django_content_type_id_seq        | sequence |                   |                   |
 public | django_migrations                 | table    |                   |                   |
 public | django_migrations_id_seq          | sequence |                   |                   |
 public | django_session                    | table    |                   |                   |
(21 rows)

You can operate the database by actually entering the SQL statement.

dry-brook-87010::DATABASE=> select * from blog_contents;
 id |   title    |       word_text        |       date_time
----+------------+------------------------+------------------------
  1 |Hello|It was a nice day today.| 2017-07-15 19:28:33+00
(1 row)

Summary

This time, I made the management page available, created a new app and its model, and even managed the database on Heroku. Next time, I will create a template and make it possible to publish the blog I made this time to the outside. (plans)

reference

Introduction to Python How to use Django (Part 11) Use the model.

Recommended Posts

Deploy the Django app on Heroku [Part 2]
Deploy the Django app on Heroku [Part 1]
Deploy the Flask app on Heroku
Deploy the Flask app on heroku
Deploy masonite app on Heroku 2020
Miscellaneous notes about deploying the django app on Heroku
Deploy your Django application on Heroku
Deploy Flask app on heroku (bitterly)
Django Heroku Deploy 1
Django Heroku Deploy 2
Deploy Django api on heroku (personal note)
How to deploy a Django app on heroku in just 5 minutes
Deploy a Django application on Google App Engine (Python3)
Deploy a Django app made with PTVS on Azure
Deploy django project to heroku
How to deploy the easiest python textbook pybot on Heroku
(Failure) Deploy a web app made with Flask on heroku
Django --Overview the tutorial app on Qiita and add features (2)
Run the app with Flask + Heroku
python + django + scikit-learn + mecab (1) on heroku
Implement a Django app on Hy
Detect app releases on the App Store
Try Ajax on the Django page
python + django + scikit-learn + mecab (2) on heroku
Publish DJango page on heroku: Practice
Deploy the Django tutorial to IIS ①
Django blog on heroku: login implementation
[Python + heroku] From the state without Python to displaying something on heroku (Part 1)
[Python + heroku] From the state without Python to displaying something on heroku (Part 2)
[Django] Error encountered when deploying heroku Part 2
[Django] Trouble encountered when deploying heroku Part 1
Change the order of PostgreSQL on Heroku
Heroku deployment of the first Django app that beginners are addicted to
Build your Django app on Docker and deploy it to AWS Fargate
Django begins part 1
Redis on Heroku
Publish django project developed in Cloud9 on heroku
Exclusive release of the django app using ngrok
Django begins part 4
shimehari on heroku
Deploy Django apps on Ubuntu + Nginx + MySQL (Build)
I want to upload a Django app to heroku
When I deployed the Django app to Heroku, I got Module Not Found: <project-name> .wsgi.
Run the flask app on Cloud9 and Apache Httpd
I tried running the app on the IoT platform "Rimotte"
How to deploy a Django application on Alibaba Cloud
Create a Todo app with the Django REST framework
Publish your Django app on Amazon Linux + Apache + mod_wsgi
I tried python on heroku for the first time
Memo of deploying Django × Postgresql on Docker to Heroku
Deploy an existing app with docker + pyenv-virtualenv + uwsgi + django
Make the model a string on a Django HTML template
Django page released on heroku: Preparation my addictive point
How to use Django on Google App Engine / Python
Django-Overview the tutorial app on Qiita and add features (1)
Install django on python + anaconda and start the server
Deploy a web app created with Streamlit to Heroku
Launch my Django app
heroku deployment memo (Django)
Initialize your Django app
Celery notes on Django