我正在建立一个网站,用户可以在其中发布和查看他们的帖子。主页上有主题,当您转到它们时,应该会打开一个包含该主题条目的动态 URL。但是当进入任何条目时,我得到一个错误 - 类型对象'描述'没有属性'entry_set'。
视图.py
from django.shortcuts import render
from .models import Topic, Description
def index(request):
topic = Topic.objects.all
context = {'topic': topic}
return render(request, 'Menu/Menu.html', context)
def description(request, topic_id):
topic = Topic.objects.get(id=topic_id)
description = Description.entry_set.all
context = {'description': description, 'topic': topic}
return render(request, 'Menu/Description.html', context)
模型.py
from django.db import models
class Topic(models.Model):
text = models.CharField(max_length=100)
def __str__(self):
return self.text
class Description(models.Model):
topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
text = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = 'Descriptions'
def __str__(self):
if len(self.text) > 10:
return f'{self.text[:10]}...'
else:
return self.text