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]
I've even reached the point where the Django app's start page (It Work page) is displayed on Heroku.
** My environment **
macOS Sierra 10.12.5 Python 3.6.1 virtualenv 15.1.0
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
This time, we will actually create an app Blog that adds the title, content, and date.
** 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 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
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)
$ 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
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.
$ git add .
$ git commit -m "I created a blog app"
$ git push heroku master
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
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
** 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.
** Add content ** The fields will be displayed as set in [Contents class](#Create new model), so enter them appropriately and click "Save".
** Check on the model management screen ** OK if the content you added earlier is displayed
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.
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)
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)
Introduction to Python How to use Django (Part 11) Use the model.
Recommended Posts