Django和外键

时间:2016-03-20 15:31:58

标签: python django foreign-keys

我正在尝试在一个页面中创建博客:文章和评论在同一页面上。这是为了理解Django如何使用外键。

实际上,我可以在同一页面上显示所有文章,但我不知道如何显示每篇文章的每条评论,因为我不知道如何获得与好的文章ID相关的好评论ID。 / p>

model.py:

#-*- coding: utf-8 -*-

from django.db import models

# Create your models here.
class Article(models.Model):
    titre = models.CharField(max_length=100)
    auteur = models.CharField(max_length=42)
    contenu = models.TextField(null=True)
    date = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name="Date de parution")

class Commentaire(models.Model):
    auteur = models.CharField(max_length=42)
    contenu = models.CharField(max_length=100)
    date = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name="Date de parution")
    article = models.ForeignKey('Article')  

views.py:

#-*- coding: utf-8 -*-

from django.shortcuts import render
from django.http import HttpResponse
from blog.models import Article, Commentaire

# Create your views here.
def actualite(request, id):
    article = Article.objects.all()
    commentaire_article = Commentaire.objects.filter(article=**ARTICLE ID ???**)

    return render(request, 'blog/templates/home.html', locals())

template home.html:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<ul>
    <h1>Liste des articles :</h1>
    {% for article in article %}
        <li>
            Titre : {{ article.titre }}<br />
            Contenu : {{ article.contenu }}<br />
            Auteur : {{ article.auteur }}<br />
            Date : {{ article.date }}<br />
            Commentaire : 
            {% for commentaire_article in commentaire_article %}
            <ul>
                <li>
                    De : {{ commentaire_article.auteur }}<br />
                    {{ commentaire_article.contenu|default:"Aucun commentaire" }}
                </li>
            </ul>
            {% endfor %}
        </li>
        <hr>
    {% endfor %}
</ul>
</body>
</html>

1 个答案:

答案 0 :(得分:4)

无需在视图/模板中添加commentaire_article。您可以通过访问其反向ForeignKey

来访问每篇文章的评论

您可以article.commentaire_set.all()访问文章的评论。

在您看来,做这样的事情

def actualite(request, id):
    articles = Article.objects.all()

    return render(request, 'blog/templates/home.html', {'articles': articles})

我认为没有将id传递给视图,因为您列出了所有文章。如果只想显示一篇文章,则可以改为使用.get()

在你的模板中,写下这样的内容以获得评论。

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<ul>
    <h1>Liste des articles :</h1>
    {% for article in articles %}
        <li>
            Titre : {{ article.titre }}<br />
            Contenu : {{ article.contenu }}<br />
            Auteur : {{ article.auteur }}<br />
            Date : {{ article.date }}<br />
            Commentaire : 
            {% for commentaire_article in article.commentaire_set.all %}
            <ul>
                <li>
                    De : {{ commentaire_article.auteur }}<br />
                    {{ commentaire_article.contenu|default:"Aucun commentaire" }}
                </li>
            </ul>
            {% endfor %}
        </li>
        <hr>
    {% endfor %}
</ul>
</body>
</html>