2013-10-01 26 views
0

我有一個根據日期提交表單的代碼。每當我使用分頁上formit顯示錯誤分頁在django中無法正常工作

"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----------------> 

回答

2

分頁Django的正常工作,這是你的代碼那就是問題所在。

出於某種原因,您使用POST來發送原始搜索變量,但隨後創建分頁鏈接,只是使用頁碼進行GET操作。當然,Django無法知道您以前的搜索條件是什麼,因爲您沒有將它們發送到POST數據 - 因此是錯誤。

執行此操作的正常方法是通過GET發送原始搜索請求 - 無論如何這是最佳實踐,因爲搜索不會修改數據。然後,在所有分頁鏈接中包含這些相同的變量,只需更換頁碼即可。