python - Django 和外键

标签 python django foreign-keys

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

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

模型.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')  

View .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())

模板 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>

最佳答案

无需在 View /模板中添加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 传递给 View 没有用,因为您要列出所有文章。如果只想显示一篇文章,可以使用 .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>

关于python - Django 和外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36115723/

相关文章:

python - 如何使用 PyQt5 在 qml 中设置值?

javascript - 如何将 Django 模板中的内容替换为其他内容?

sql - 在一张表中包含多个外键是一种好习惯吗?

mysql - 无法在 PhpStorm 中创建外键

SQL : Adding foreign key on existing composite key

python - 在 Python 中分析时间序列 - pandas 格式错误 - statsmodels

python - Django 模板 : static files in app directory

python - 如何从 django 中的表中检索评论回复

python - 如何在 django 模型中初始化与自身无关的 ManyToMany 字段(对象级别)?

python - 使用 wxPython 配置 Eclipse