2016-03-20 25 views
1

私は1つのページでブログを作ろうとしています。記事とコメントは同じページにあります。これは、Djangoが外部キーでどのように動作するかを理解することです。Djangoと外部キー

実際、同じページにすべての記事を表示できますが、良い記事IDに関連付けられた良いコメントIDを取得する方法がわからないため、各記事ごとに各コメントを表示する方法がわかりません。

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

テンプレート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> 

答えて

4

ビュー/テンプレートでcommentaire_articleを追加する必要はありません。逆のForeignKeyにアクセスして各記事のコメントにアクセスできます。

記事のコメントにはarticle.commentaire_set.all()でアクセスできます。あなたのビューで

は、私はあなたがすべての記事をリストアップしているので、ビューにidを渡すのない使用を見ないこの

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

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

ような何かを行います。 1つの記事のみを表示する場合は、代わりに.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>