本文介绍了函数对象没有属性"entry_set"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Django的一些事情正如照片所示我不知道为什么会有AttributeError

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)

推荐答案

问题可能出在您的主题函数中.您将一个主题分配给 topic 变量,然后尝试从名为 topic 的变量而不是 topics 的entry_set中获取一个条目.由于您只有一个主题,因此将 topics 变量更改为单个 topic :

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)

这篇关于函数对象没有属性"entry_set"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 17:38