python - 详细视图中的分页
问题描述
我在我的博客中使用我的帖子的详细视图,每篇帖子都有评论,所以我想对它们进行分页,但我不知道该怎么做,因为我请求了帖子模型。我知道如何在功能视图中执行此操作,但在详细视图中不知道...
##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
# ...
因此,我们page
从参数中获取self.request.GET
参数,然后我们制作一个Paginator
并相应地分页。您可能还应该根据某些字段对评论进行排序。现在评论可以以任何顺序出现,因此下一页可以包含上一页出现的评论,等等。
因此,该comments
变量是一个分页对象,您可以像在基于函数的视图中一样呈现它。
请注意,您可以使用comment_set
(或者如果您设置另一个related_name
名称)来访问与Post
对象相关的属性集。
话虽这么说,也许这更像是 a ListView
over the comments,或者 a FormView
,因为您包含 a Form
to comment。
推荐阅读
- android - Constraintlayout(in RelativeLayout) layout_alignParentBottom 从底部切割(在应用程序的默认模式下)
- java - 来自长时间运行的 Java 服务器的 JDWP 内存不足错误
- node.js - AWS Lambda 上传到 S3 超时
- javascript - 未使用“createNavigationContainer”时出现“createNavigationContainer()”错误
- python - 解析 AWS Lambda JSON
- ios - 应用程序在使用 PJSIP 连续暂停和启动视频流时崩溃
- reactjs - 在 React 应用程序中使用 ActiveMQ
- laravel - Laravel 使用什么关系来查看来自另一个模型和列的数据
- c# - 在 ASP 控制器中创建数据库上下文实例是否有意义?
- raspberry-pi3 - 如何使用 Raspbian 在 Raspberry Pi 上设置联网的 Brother 打印机 (Brother_DCP_L2520DW_series)?