Help us understand the problem. What is going on with this article?

サイト見てDjango2試してみたけど、できなかった人向け

More than 1 year has passed since last update.

はじめに

PythonのWebアプリケーション(Django)を初心者にもわかりやすく解説 (https://qiita.com/yoshizaki_kkgk/items/f3c79e38b802c7fc8863)
こちらのサイトでDjangoの導入をやっても、自分の環境では2つ上手くいかなかった点があったのでまとめました。

開発環境

・OS: macOS High Sierra(10.13)

$ python3 -V
Python 3.6.5 :: Anaconda, Inc.
django.get_version()
'2.1.1'

モデルの作成

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

class Post(models.Model):
    author = models.ForeignKey('auth.User')
    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

とありますが、Django2になってから以下のようなエラーが出ます。

TypeError: __init__() missing 1 required positional argument: 'on_delete'

そこで、以下のように書き換えましょう。
on_deleteを追加してあげています。

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

ルーティングの設定

myapp/urls.py
from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'', include('blog.urls')),
]

このままだと、以下のようにテンプレートが見つからないとか言われてしまいます。

TemplateDoesNotExist at /

そこで、こんな感じに書き換えてあげましょう。
adminのurlをpathに変更しています。
すると、ちゃんとテンプレートを見つけてくれます。

myapp/urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'', include('blog.urls')),
]
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした