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

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

  • 写回答

3条回答

  • Watch_dou 2019-01-09 13:38
    关注

    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)

    评论

报告相同问题?

悬赏问题

  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元