1。 实现显示单个主题时,点击单个主题出现以下错误
'function' object has no attribute 'entry_set'
Request Method: GET
Request URL: http://localhost:8000/topics/(%3FP1%5Cd+)/
Django Version: 2.1.4
Exception Type: AttributeError
Exception Value: 'function' object has no attribute 'entry_set'
Exception Location: D:\Python_env\learning_log\learning_logs\views.py in topic, line 22
Python Executable: D:\Python_env\learning_log\11_env\Scripts\python.exe
from django.shortcuts import render
from .models import Topic
# Create your views here.
def index(request):
return render(request, 'learning_logs/index.html')
def topics(request):
# 显示所有的主题
topics = Topic.objects.order_by('date_added')
context = {'topics': topics}
return render(request, 'learning_logs/topics.html', context)
def topic(request, topic_id):
"""显示单个主题及其所有的条目"""
topic = Topic.objects.get(id = topic_id)
entries = topic.entry_set.order_by('-date_added')
context = {'topic': topic, 'entries': entries}
return render(request, 'learning_logs/topic.html', context)
好像问题是出在entries = topic.entry_set.order_by('-date_added')
entry_set没有定义吗?
topic没有entry_set这个属性,建议调试下面函数中的entries
def topic(request, topic_id):
"""显示单个主题及其所有的条目"""
topic = Topic.objects.get(id = topic_id)
entries = topic.entry_set.order_by('-date_added')
context = {'topic': topic, 'entries': entries}
return render(request, 'learning_logs/topic.html', context)
entry_set其中的entry是指你用models创建的一张表,如果我没猜错的话你再models里并没有定义class entry():
你检查一下,leanring_logs\urls.py文件的URL映射配置对了没有。
"""定义learning_logs的URL模式"""
from . import views
from django.urls import path
app_name = 'learning_logs'
urlpatterns = [
# 主页
path(r'',views.index, name = 'index'),
# 主题列表页面(显示所有的主题)
path(r'topics/',views.topics, name ='topics'),
# 特定主题的详细页面
path(r'topics/<int:topic_id>/',views.topic, name ='topic'),
]