4

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

1 回答 1

6

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

return HttpResponseRedirect('/forum/topic/%s/?page=%s'  % (topic.slug, posts.num_pages))
于 2015-09-15T06:38:44.990 回答