2015-09-15 56 views
3

在一个简单的论坛中,我使用的是原生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)) 

其中产量:

'Page' object has no attribute 'page_range' 

我试过其他的技巧,比如:

posts = list(Post.objects.filter(topic= topic)) 

,但没有奏效。所以一无所知,欣赏你的提示。

回答

4

尝试使用num_pages。最后一页的数量应该等于页数。

return HttpResponseRedirect('/forum/topic/%s/?page=%s' % (topic.slug, posts.num_pages)) 
+0

是的,它的工作原理。谢谢! – Jand

相关问题