[英]pagination is not working properly in django
我有一個代碼根據日期提交表單。 每當我在formit上使用分頁時都會顯示錯誤
"Key 'userchoice' not found in <QueryDict: {}>"
分頁限制數據正確顯示,但是當我單擊“下一步”時,它會顯示錯誤。
這是我到目前為止所得到的:
views.py: -
def testeruser(request):
getchoice = request.POST['userchoice']
getfirstdate = request.POST['firstdate']
getseconddate = request.POST['seconddate']
# getfirstdate = '2013-09-25'
# getseconddate = '2013-09-26'
if getchoice == '0':
getdata = applicationform.objects.filter(date__gte=getfirstdate , date__lte=getseconddate)
##### PAGINATION
searchpagination = Paginator(getdata ,3)
page=request.GET.get('searchpage')
try:
searchcontacts = searchpagination.page(page)
except PageNotAnInteger:
searchcontacts = searchpagination.page(1)
except EmptyPage:
searchcontacts = searchpagination.page(searchpagination.num_pages)
if getdata:
return render_to_response('registration/search_page.html', {'getdata':getdata ,'getchoice':getchoice ,'searchcontacts': searchcontacts})
else:
return HttpResponse('NO ITEMS FOUND ON THIS DATE')
在自定義模板中: -
<form method="POST" action="/testeruser/" class="form-horizontal" name="searchform" enctype="multipart/form-data" >{% csrf_token %}
<select name="userchoice" id="client_specification" class="span2" required> <option value='-1'>Select Your Choice </option>
<option value='0'>Biddings</option>
<option value='1'>Interviews</option>
<option value='2'>Jobs</option>
</select>
From: <input type="text" class="input-xlarge" name="firstdate" id="search1" readonly="readonly" />
To: <input type="text" class="input-xlarge" name="seconddate" id="search2" readonly="readonly"/> </span>
<button class="btn btn-gebo" type="submit" name="asubmit" >Submit</button>
</form>
<!------------ PAGINATION---------------->
<div class="pagination">
<ul> {% if searchcontacts.has_previous %}
<li><a href="?searchpage={{ searchcontacts.previous_page_number }}">PREVIOUS</a></li>
{% endif %}
{% if searchcontacts.has_next %}
<li><a href="?searchpage={{ searchcontacts.next_page_number }}">NEXT</a></li>
{% endif %}
</ul>
</div>
<!------------ PAGINATION---------------->
Django中的分頁工作正常,這是你的代碼問題。
出於某種原因,您使用POST發送原始搜索變量,然后創建只使用頁碼進行GET的分頁鏈接。 當然,Django無法知道您以前的搜索條件是什么,因為您沒有在POST數據中發送它們 - 因此錯誤。
執行此操作的常規方法是通過GET發送原始搜索請求 - 無論如何這是最佳做法,因為搜索不會修改數據。 然后在所有分頁鏈接上包含相同的變量,只需替換頁碼即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.