我使用分页器来创建一系列符合特定标准的页面。
每个页面都包含一个用户可以提交的表单。
提交表单后,我希望他们留在/返回他们所在的页面。我怎么做?
我到处搜索并阅读了文档,但找不到答案。
我确定问题出在电脑前,但请帮忙:)
以下是应该这样做的视图:
def assignment(request, pk):
"""View for each assignment"""
if request.user.is_authenticated():
#### Get correct articles
assignment = get_object_or_404(Assignment, pk=pk)
country = assignment.country.ccode
start_date = assignment.start_date
end_date = assignment.end_date
articles = Article.objects.filter(country=country).filter(pub_date__range=(start_date,end_date))
paginator = Paginator(articles, 1)
#### Pagination ####
page = request.GET.get('page')
try:
articles = paginator.page(page)
except PageNotAnInteger:
articles = paginator.page(1)
except EmptryPage:
articles = paginator(page(paginator.num_pages))
#### Forms ####
if request.method == 'POST':
if 'event' in request.POST:
event_form = EventRecordForm(request.POST, prefix='event')
if event_form.is_valid():
obj = event_form.save(commit=False)
obj.article = paginator.page(page).object_list[0]
obj.classified = True
obj.coder = request.user.coder
obj.save()
return HttpResponseRedirect(reverse ('coding:assignment', args=(pk,)))
relevance_form = RelevanceCodingRecordForm(prefix='relevance')
elif 'relevance' in request.POST:
relevance_form = RelevanceCodingRecordForm(request.POST, prefix='relevance')
if relevance_form.is_valid():
obj = relevance_form.save(commit=False)
obj.article = paginator.page(page).object_list[0]
obj.screened = True
obj.coder = request.user.coder
obj.save()
return HttpResponseRedirect(reverse ('coding:assignment', args=(pk,)))
event_form = EventRecordForm(prefix='event')
else:
event_form = EventRecordForm(prefix='event')
relevance_form = RelevanceCodingRecordForm(prefix='relevance')
else:
print ERROR
return render(request, 'coding/assignment.html',
{'articles':articles,'assignment':assignment,'event_form':event_form,'relevance_form':relevance_form})
编辑:
相关建议我附加?next={{request.path}}
到我的表单操作。
但是,这不起作用,因为分页器页面不包含在路径中。做他建议的事情:
http://127.0.0.1:8000/coding/assignment/1/?next=/coding/assignment/1/
虽然我确实给了分页器第二次看,并认为这?page={{ articles.number }}
应该可行,因为在文档中的示例中,他们做了这样的链接:
<a href="?page={{ articles.next_page_number }}">next</a>
但是,这不起作用。
还有其他想法吗?