u011318077
Felix-微信(Felixzfb)
采纳率0%
2018-12-26 21:33

Django实现Web应用显示单个主题时出现错误: 'function' object has no attribute 'entry_set'

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

  1. 源代码如下
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没有定义吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • qq_40816856 qq_40816856 2年前

    entry_set其中的entry是指你用models创建的一张表,如果我没猜错的话你再models里并没有定义class entry():

    点赞 1 评论 复制链接分享
  • u012111465 Watch_dou 2年前

    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)

    点赞 评论 复制链接分享

相关推荐