Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

django - The function object has no attribute 'entry_set'

enter image description here

enter image description here

Something about Django Just as the photo show I don't know why there is has a AttributeError

from django.shortcuts import render
from .models import Topic
def topic(request, topic_id):
   topics = 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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is probably in your topic function. You assign one topic to the topics variable and then try to get an entry_set off a variable called topic instead of topics. Since you're only getting one topic it would make more sense to change the topics variable to singular topic:

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...