分页详细视图



我在博客中使用帖子的详细视图,每个帖子都有评论,所以我想打开它们,但是我不konw如何做帖子模型。我知道如何在功能视图中进行操作,但详细视图...

##view :
class PostDetailView(DetailView):
    model = Post
    def get_context_data(self, **kwargs):
        context = super(PostDetailView, self).get_context_data(**kwargs)
        context['comments'] = Comment.objects.filter(post_id=self.object.id).all()
        context['comments_number'] = Comment.objects.filter(post_id=self.object.id).count()
        context['form'] = CommentForm()
        return context

    def post(self, request, pk):
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = Post.objects.get(id=pk)
            comment.user = request.user
            comment.save()
            post = Post.objects.get(pk=pk)
            post.comments_nmb+=1
            post.save()
        return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

##template:
{% extends "blog/base.html" %}
{% block content %}
<article class="media content-section">
<img class="rounded-circle article-img" src="{{ object.author.profile.image.url }}">
<div class="media-body">
  <div class="article-metadata">
    <a class="mr-2" href="{% url 'user-posts' object.author.username %}">{{ object.author }}</a>
    <small class="text-muted">{{ object.date_posted|date:"F d, Y" }}</small>
    {% if object.author == user %}
      <div>
        <a class="btn btn-secondary btn-sm mt-1 mb-1" href="{% url 'post-update' object.id %}">Update</a>
        <a class="btn btn-danger btn-sm mt-1 mb-1" href="{% url 'post-delete' object.id %}">Delete</a>
      </div>
    {% endif %}
  </div>
  <h2 class="article-title">{{ object.title }}</h2>
  <p class="article-content">{{ object.content }}</p>
  <p>{{comments_number}} Comments</p>
 {%  for comment in comments %}
 <div class="media">                            
                        <a class="float-left">
                          <img class="rounded-circle account-img" src="{{ comment.user.profile.image.url }}">
                        </a>
                        <div class="media-body">
                          <h4 class="media-heading ">{{ comment.user.username }}</h4>
                          {{comment.text}}
                        </div>
                        <p class="float-right"><small>{{ comment.date}}</small></p>
                      </div>
{% endfor %}
</div>
</article>
{% endblock content %}

如何在for循环中进行评论?

几乎完全相同:

from django.core.paginator import Paginator
class PostDetailView(DetailView):
    model = Post
    def get_context_data(self, **kwargs):
        context = super(PostDetailView, self).get_context_data(**kwargs)
        page = self.request.GET.get('page')
        comments = Paginator(self.object.comment_set.all(), 25)
        context['comments'] = comments.get_page(page)
        context['comments_number'] = self.object.comment_set.count()
        context['form'] = CommentForm()
        return context
    # ...

因此,我们从self.request.GET参数获得page参数,然后制作Paginator并相应地页面。您可能还应该根据某些字段订购评论。目前,这些评论可以按任何顺序显示,因此下一页可以包含上一页出现的注释,等等。

因此,comments变量是分页对象,您可以像基于功能的视图一样渲染它。

请注意,您可以使用comment_set(或者设置另一个related_name,该名称)访问与Post对象相关的属性集。

说,也许这是评论或FormViewListView,因为您包括Form来评论。

最新更新