我使用paginator創建一系列滿足特定條件的頁面。
每個頁面都包含一個用戶可以提交的表單。
表單提交後,我希望他們留在/返回到他們所在的頁面。 我該怎麼做?
我到處搜索並閱讀文檔,但無法找到答案。
我相信這個問題是在電腦前,但請幫助:)表單提交後重定向到分頁頁面 - Django
下面是應該這樣做的觀點:
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}}
附加到我的表單操作中。
但這不起作用,因爲paginator頁面不包含在路徑中。 做什麼,他認爲出現這種情況: 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>
然而,這並未沒有工作。
有沒有其他想法?
不幸的是,它不在request.path中,我更新了我的問題。 但是,無論如何,謝謝! :) – LukasKawerau 2013-05-04 14:14:43