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没有定义吗?