[django]详情页列表页

详情页列表页

列表页展示titile--这个模型的部分字段
详情页展示这个模型的所有字段

我想看下related_name这个从主表取子表数据

取数据--官网投票例子

https://docs.djangoproject.com/en/2.1/intro/tutorial02/

polls/models.py
from django.db import models


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
polls/views.py
from django.shortcuts import get_object_or_404, render

from .models import Question
# ...
def detail(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/detail.html', {'question': question})

类别 tag 文章/出版社 作者 图书

小结

url
    列表/详情
        1.(文章)一个model情况
view
    列表/详情
        1.(文章)一个model情况
            问题: titile
            选项: choice: get_object_or_404
        2.问题选项.(主表.次表_set)
model
    related_name

posted @ 2018-09-18 09:36  mmaotai  阅读(1774)  评论(0编辑  收藏  举报