Django and foreign key

user6089877

I'm trying to make a blog in one page : articles and comments are on the same page. It's to understand how Django work with foreign key.

Actually, I can display all articles on the same page, but I don't know how to display each comments for each articles because I don't know how to get the good comments id associated with the good article 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())

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

There is no need to add commentaire_article in the view/ template. You can access comments of each articles by accessing its reverse ForeignKey.

You can access comments of articles by article.commentaire_set.all().

In your view, do something like this

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

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

I see no use of passing id to the view because you are listing all articles. If want to display only one article, you can use .get() instead.

In your template, write something like this to get comments.

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related