Django-如何查看子类的通用视图

时间:2018-06-08 09:53:04

标签: python django django-models django-views

基本上我要做的就是查看所有评论并附上某篇文章

这是我的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'),

]

最后是我的html文件

{% 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 %}

我刚开始使用通用视图而且他们的喉咙很疼,但我觉得它们有多强大,我不确定使用DetailView评论部分是否是我的理想情况情况。

1 个答案:

答案 0 :(得分:2)

首先要做的事情:请为您的班级使用专有名称。所有课程必须以大写字母开头,因此您需要使用CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) appBarLayout1.getLayoutParams(); 代替article

现在,由于每条评论都属于一篇文章,我不认为您需要为每条评论添加个人Article,但评论只需要包含在DetailView中。此外,您的ArticleDetailView不需要带有排序的查询集; ArticleDetailView返回一个对象,因此不需要排序。以下是应该如何改为:

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

然后你会有一个DetailView这样的模板:

    {{ 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 %}

这将是关于每篇文章的一般信息及其评论。

最后,如果你想了解更多关于基于django类的观点,我写了一篇非常全面的指南@ https://spapas.github.io/2018/03/19/comprehensive-django-cbv-guide/