在django中使用分页时如何返回最后一页?

Jan*_*and 4 django django-pagination

在一个简单的论坛中,我使用本机 django分页我希望用户在他们发布后被定向到线程中的最后一页。

这是视图

@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)) 
Run Code Online (Sandbox Code Playgroud)

其中产生:

'Page' object has no attribute 'page_range'
Run Code Online (Sandbox Code Playgroud)

我尝试了其他技巧,例如:

posts = list(Post.objects.filter(topic= topic))
Run Code Online (Sandbox Code Playgroud)

但没有一个工作。所以让我一无所知,并感谢您的提示。

Rod*_*ier 6

尝试使用num_pages. 最后页码应等于页数。

return HttpResponseRedirect('/forum/topic/%s/?page=%s'  % (topic.slug, posts.num_pages))
Run Code Online (Sandbox Code Playgroud)