【问题标题】:Django and foreign keyDjango 和外键
【发布时间】:2016-03-20 15:31:58
【问题描述】:

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

其实我可以在同一个页面上显示所有文章,但是我不知道如何显示每个文章的每个 cmets,因为我不知道如何获取与好文章 id 关联的好 cmets 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')  

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

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

【问题讨论】:

    标签: python django foreign-keys


    【解决方案1】:

    不需要在视图/模板中添加commentaire_article。您可以通过访问其反向ForeignKey来访问每篇文章的cmet。

    您可以通过article.commentaire_set.all()访问文章的cmets。

    在你看来,做这样的事情

    def actualite(request, id):
        articles = Article.objects.all()
    
        return render(request, 'blog/templates/home.html', {'articles': articles})
    

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

    在您的模板中,编写类似这样的内容来获取 cmets。

    <!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>
    

    【讨论】:

      猜你喜欢
      • 2020-11-13
      • 1970-01-01
      • 1970-01-01
      • 2020-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-01
      • 2010-11-10
      相关资源
      最近更新 更多