AhmedKhalifa
AhmedKhalifa

Reputation: 3

Django- How to view Generic views of a child class

Basically what I am trying to do is view all the comments accompanied with a certain post

This is my views.py

from django.views import generic

from .models import comment, article

class IndexView(generic.ListView):
    template_name = 'newspaper/newspaper.html'
    context_object_name = 'articles'

    def get_queryset(self):
        return article.objects.order_by('-date')


class ArticleDetailsView(generic.DetailView):
    template_name='newspaper/details.html'
    context_object_name = 'articles'

    def get_queryset(self):
        return article.objects.order_by('-date')


class CommentsDetailsView(generic.DetailView):
    template_name='newspaper/details.html'
    context_object_name = 'comments'

    def get_queryset(self):
        return comment.objects.all()

models.py

from django.db import models
from datetime import datetime

class article(models.Model):
    title= models.CharField(max_length=150)
    body= models.TextField()
    date= models.DateField()
    author= models.CharField(max_length=150, default='')

    def __str__(self):
        return self.title + ' - ' + str(self.author)


class comment(models.Model):
    main_article=models.ForeignKey('article',on_delete=models.CASCADE)
    text=models.TextField(default='Empty reply')
    date = models.DateTimeField(default=datetime.now, blank=True)
    def __str__(self):
        return self.text

urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^(?P<pk>[0-9]+)$',views.ArticleDetailsView.as_view(),name='articledetails'),
     url(r'^(?P<main_article_id>[0-9]+)$',views.CommentsDetailsView.as_view(),name='commentdetails'),

]

and finally my html file

{% extends "header.html" %}

{% block title %}
Article Details
{% endblock %}

{% block content %}
{% if articles %}

<div class="panel panel-primary">
    <div class="panel-heading">
    <h2>{{ articles.title}}  </h2>
    </div>
  <div class="panel-body">
    <h4>{{ articles.body}}  </h4>
  </div>
  <div class="panel-footer">
    <h5><i> Posted on: {{ articles.date}} </i> </h5>
    <h6 class="text-right"><i>By: {{articles.author}}</i></h6>
    </div>
</div>

<br>

<button class="btn btn-primary" type="button" >
  Replies <span class="badge">{{ comments.count }}</span>
</button>

{% for y in comments %}
<div class="well">
<h5>{{ y.text}}  </h5>
<h5><i>On: {{ y.date}}  </i></h5>
</div>
<br>
{% endfor %}
{% else %}
<div class="alert alert-danger" role="alert">There is no articles with the ID</div>

{% endif %}
{% endblock %}

I am just starting with generic views and they have been a pain in my throat but I could feel how powerful they are, I am not sure if using the DetailView for the comments section is the ideal case for my situation.

Upvotes: 0

Views: 348

Answers (1)

Serafeim
Serafeim

Reputation: 15084

First things first: Please user proper names for your classes. All classes must begin with a capital letter so instead of article you'll need to use Article.

Now, since each comment will belong to an article, I dont think that you need an individual DetailView for each comment but the comments need to just be included with the ArticleDetailView. Also, your ArticleDetailView does not need a queryset with sorting; the DetailView returns one object so no sorting is needed. Here's how to should be instead:

class ArticleDetailView(generic.DetailView):
    context_object_name = 'article'

Then you'll have an article_detail.html template like this:

    {{ article.title}}
    {{ article.body}}
    ... etc I won't include everything
    and for the comments:

    {% for comment in article.comment_set.all %}
        {{ comment.text }} on {{ comment.date }}
    {% endfor %}

This will the general info about each article along with its comments.

Finally, if you want to learn more about django class based views, I've written a very comprehensive guide @ https://spapas.github.io/2018/03/19/comprehensive-django-cbv-guide/

Upvotes: 4

Related Questions