我正在写一个简单的博客,我需要为每个帖子分页评论,但我不知道如何做到这一点。谢谢帮助!有我的模型:
class Post(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
short_description = models.CharField(max_length=230, default='Post')
text = models.TextField()
is_published = models.BooleanField(default=False)
def __str__(self):
return self.title
class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE)
username = models.CharField(max_length=35)
comment = models.CharField(max_length=120)
is_published = models.BooleanField(default=False)
def __str__(self):
return self.comment
以下是我的看法:
class PostDetail(generic.DetailView):
template_name = 'user/post.html'
model = Post
def comment_view(request, pk):
form = CommentCreateForm()
if request.method == 'POST':
form = CommentCreateForm(request.POST)
if form.is_valid():
username = request.user
comment = form.cleaned_data['comment']
Comment.objects.create(username=username, comment=comment, post_id=pk)
return redirect('PostDetail', pk)
return render(request, 'user/comment_view.html', {'form': form})
这是我的模板:
{ object.title }}
{% if object.author.id %}
<a href="{% url 'public_profile' object.author.id %}">{{ object.author }}</a>
{% endif %}
<p>{{ object.text }}</p>
<h4>Comments</h4>
{% for comment in object.comment_set.all %}
{{ comment.username }}
<p>{{ comment }}</p>
{% endfor %}
<p><a href="{% url 'comment_view' object.id %}">Leave comment</a></p>
注解分页是真实的的吗?
我应该如何在detail_view中对注解分页?
1条答案
按热度按时间ni65a41a1#
分页就像对数据进行切片,所以你能做的就是过滤所有的数据,然后对其进行切片。
用于获取下一个前一个url的函数