问题遇到的现象和发生背景
我在参考python从入门到实践一书Django入门中遇到了困难:
我已经完成了topic.html的制作,可在运行时遇到了django报错:
用代码块功能插入代码,请勿粘贴截图
models.py
from django.db import models
class Topic(models.Model):
id = models.AutoField(primary_key=True)
text = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.text
class Entery(models.Model):
id = models.AutoField(primary_key=True)
topic = models.ForeignKey(Topic,on_delete=models.CASCADE,related_name="enteries")
text = models.TextField()
date_added = models.DateTimeField(auto_now_add = True)
class Meta:
verbose_name_plural = "entries"
def __str__(self):
return self.text[:50]+"..."
# Create your models here.
admin.py
from django.contrib import admin
from learning_logs.models import Topic,Entery
admin.site.register(Topic)
admin.site.register(Entery)
learning_log\urls.py
from django.conf.urls import include
from django.urls import re_path as url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),url(r"", include(("learning_logs.urls","learning_logs"), namespace = "learning_logs"))
]
learning_logs\urls.py
from django.urls import re_path as url
from . import views
urlpatterns = [
# Home page.
url(r'^$', views.index, name='index'),
# Show all topics.
url(r'^topics/$', views.topics, name='topics'),
# Detail page for a single topic.
url(r'^topics/(?P<topic_id>\d+)/$', views.topic, name='topic'),
]
views.py
from django.shortcuts import render
from .models import Topic,Entery
#这里原本是绝对地址,我觉得有关隐私,把项目名称即以前的地址删了,如果需要调试的化可以自己加
def index(request):
return render(request, r"templates/index.html")
def topics(request):
topics = Topic.objects.order_by("date_added")
context = {"topics": topics}
return render(request, r"learning_logs\templates\topics.html",
context)
def topic(request, topic_id):
topic = Topic.objects.get(id=topic_id)
entries = topic.enteries.order_by("-date_added")
content = {"topic": topic, "entries": entries}
return render(request, r"learning_logs\templates\topic.html",
content)
运行结果及报错内容
OperationalError at /topics/1/
no such column: learning_logs_entery.date_added
我的解答思路和尝试过的方法
去过django官网,用有道之后也看不懂,问了度娘好几天,没找到一个我看得懂的(无语)
我想要达到的结果
显示出topic下的对应entery