首页 > 解决方案 > 详细视图中的分页

问题描述

我在我的博客中使用我的帖子的详细视图,每篇帖子都有评论,所以我想对它们进行分页,但我不知道该怎么做,因为我请求了帖子模型。我知道如何在功能视图中执行此操作,但在详细视图中不知道...

##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 循环中为评论分页?

标签: pythondjangowebdjango-modelspagination

解决方案


几乎完全相同的方式:

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

    # ...

因此,我们page从参数中获取self.request.GET参数,然后我们制作一个Paginator并相应地分页。您可能还应该根据某些字段对评论进行排序。现在评论可以以任何顺序出现,因此下一页可以包含上一页出现的评论,等等。

因此,该comments变量是一个分页对象,您可以像在基于函数的视图中一样呈现它。

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

话虽这么说,也许这更像是 a ListViewover the comments,或者 a FormView,因为您包含 a Formto comment。


推荐阅读