在 Django 中使用分页时如何 return 最后一页?
How to return the last page when using pagination in django?
在一个简单的论坛中,我使用的是本机 django Pagination我希望用户在发帖后被定向到线程中的最后一页。
这是景色
@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))
但 none 有效。所以一无所知,感谢您的提示。
尝试使用 num_pages
。最后页码应等于页数。
return HttpResponseRedirect('/forum/topic/%s/?page=%s' % (topic.slug, posts.num_pages))
在一个简单的论坛中,我使用的是本机 django Pagination我希望用户在发帖后被定向到线程中的最后一页。
这是景色
@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))
但 none 有效。所以一无所知,感谢您的提示。
尝试使用 num_pages
。最后页码应等于页数。
return HttpResponseRedirect('/forum/topic/%s/?page=%s' % (topic.slug, posts.num_pages))