[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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM