在Django中使用分页时如何返回最后一页? [英] How to return the last page when using pagination in django?
本文介绍了在Django中使用分页时如何返回最后一页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在一个简单的论坛中,我使用的是本地django 分页我希望用户在发布到该页面后将其定向到该线程的最后一页.
In a simple forum, I'm using native django Pagination I'd like users to be directed to the last page in a thread after they posted there.
这是视图
@login_required
def topic_reply(request, topic_id):
tform = PostForm()
topic = Topic.objects.get(pk=topic_id)
args = {}
posts = Post.objects.filter(topic= topic)
posts = Paginator(posts,10)
if request.method == 'POST':
post = PostForm(request.POST)
if post.is_valid():
p = post.save(commit = False)
p.topic = topic
p.title = post.cleaned_data['title']
p.body = post.cleaned_data['body']
p.creator = request.user
p.save()
return HttpResponseRedirect('/forum/topic/%s/?page=%s' % (topic.slug, posts.page_range[-1]))
else:
args.update(csrf(request))
args['form'] = tform
args['topic'] = topic
return render_to_response('myforum/reply.html', args,
context_instance=RequestContext(request))
哪个产量:
'Page' object has no attribute 'page_range'
我尝试了其他技巧,例如:
I tried other tricks like:
posts = list(Post.objects.filter(topic= topic))
,但是没有一个起作用.所以一无所知,感谢您的提示.
but none worked. So left clueless and appreciate your hints.
推荐答案
尝试使用num_pages
.最后页数应等于页数.
Try using num_pages
. The last page number should be equal to the number of pages.
return HttpResponseRedirect('/forum/topic/%s/?page=%s' % (topic.slug, posts.num_pages))
这篇关于在Django中使用分页时如何返回最后一页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文