2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【注意点】Django Girls Tutorialのエラーと対処法

Last updated at Posted at 2019-04-27

Django Girls Tutorial

チュートリアルを進めていく上で、いくつかエラーがありましたので、その対処法を簡単に書いてみます。

なお、環境はPython3.7.3Django 2.2になりますのでご注意ください。

・Django Girls Tutorial
https://djangogirlsjapan.gitbook.io/workshop_tutorialjp/

プロジェクトを作成しよう!

Herokuにデプロイする際に、git push heroku masterを実行するとエラーになります。
チュートリアルにはpip install django==1.11とありますが、こちらは最新バージョンでないため、下記のように最新バージョンをインストールしましょう。

$ pip install --upgrade setuptools
$ pip install --upgrade pip
$ pip install django

参考:
https://teratail.com/questions/139826
https://qiita.com/snowman_mh/items/c3f9f6500a01a267fea6

Djangoモデル

Django2.0から必須になったon_deleteをブログポストモデルの作成で、blog/models.pyに追加します。今回はon_delete=models.CASCADEにしてます。

blog/models.py
from django.db import models
from django.utils import timezone

class Post(models.Model):
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    text = models.TextField()
    created_date = models.DateTimeField(
            default=timezone.now)
    published_date = models.DateTimeField(
            blank=True, null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title

参考:
https://qiita.com/ebichiki/items/387579242a17bb2902ca

on_deleteはいくつか種類がありますので、気になる方は参考にどうぞ。
https://www.djangobrothers.com/blogs/on_delete/

Djangoのインストール

requirement.txtの最後にpsycopg2==2.5.4を追加してとありますが、こちらもバージョンが最新ではないので下記のようにアップデートしてrequirement.txtを変更します。

$ pip install --upgrade psycopg2
$ pip freeze > requirements.txt

requirement.txtは下記のように変更していれば大丈夫です。

requirement.txt
dj-database-url==0.5.0
Django==2.2
gunicorn==19.9.0
pytz==2019.1
sqlparse==0.3.0
whitenoise==4.1.2
psycopg2==2.8.2

参考:
https://qiita.com/mimizuk/items/5a6f2ea4136b797b9924

デプロイ

Pythonのバージョンをruntime.txtに書きますが、チュートリアルとおりpython-3.5.2ではエラーが起こります。

こちらは、いま使用中のバージョンを書きますので、python-3.7.3になります。

WhiteNoise

デプロイする際に下記コードを追加してくださいとありますが、こちらは必要ありませんので、追加しなくて大丈夫です。

mysite/wsgi.py
from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)

下記のようになっていれば、問題ありません。

mysite/wsgi.py
import os
from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
application = get_wsgi_application()

参考:
https://takesso-tech.hatenablog.com/entry/2018/08/13/191246#Psycopg

テンプレートを拡張しよう

Djangoをバージョンアップせずに進めていった場合、投稿タイトルをクリックしても、各投稿が表示されないと思います、その場合はblog/urls.pyを変更します。

変更前

blog/urls.py
from django.conf.urls import include, url
from . import views
from django.urls import path

urlpatterns = [
    url(r'^$', views.post_list),
    url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail),
    url(r'^post/new/$', views.post_new, name='post_new'),
    url(r'^post/(?P<pk>[0-9]+)/edit/$', views.post_edit, name='post_edit'),
]

変更後

blog/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('', views.post_list, name='post_list'),
    path('post/<int:pk>/', views.post_detail, name='post_detail'),
    path('post/new/', views.post_new, name='post_new'),
    path('post/<int:pk>/edit/', views.post_edit, name='post_edit'),
]

参考:
https://qiita.com/massa142/items/15c130169d3bfc4a9996
https://qiita.com/shin_coding_1989/items/c93eef408cafb45dfc44

その他

Herokuにデプロイされる場合は、サポートされているバージョンを確認したほうがスムーズだと思うので、下記リンクから確認してください。

・Heroku Supported Runtime
https://devcenter.heroku.com/articles/python-support#supported-runtimes

最後に、わたしが見つけた最新のチュートリアル記事を参考までに。
http://mmtomitomimm.blogspot.com/2019/01/django-girls.html

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?