为了账号安全,请及时绑定邮箱和手机立即绑定

django 通用视图:类别中的详细信息

django 通用视图:类别中的详细信息

慕仙森 2021-07-08 17:11:27
我使用通用视图来列出我的类别。我还想显示属于这些类别的每个项目的标题。我理解 ListView 和 DetailView 的原理,但是列表中的一些细节呢?这是我的不同文件:模型.pyclass categories(models.Model):    name = models.CharField(max_length=50,unique=True)    slug = models.SlugField(max_length=100,unique=True)    def __str__(self):        return self.nameclass details(models.Model):   title = models.CharField(max_length=100)   author = models.CharField(max_length=42)   category = models.ForeignKey('categories', on_delete=models.CASCADE)    def __str__(self):        return self.title视图.pyclass IndexView(generic.ListView):    model = categories    context_object_name = "list_categories"    template_name='show/index.html'网址.pyurlpatterns = [path('', views.IndexView.as_view(), name='index'),]索引.html{% load static %}<p>These is a list of categories</p>{% for category in list_categories %}    <div class="article">       <h3>{{ category.name }}</h3>        {% for title in category.detail %}            <p> {{title}} </p>        {% endfor %}    </div>{% endfor %}
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 138 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号