简体   繁体   中英

pagination is not working properly in django

I have a code that submit the form according to the date. Whenever I use pagination on the formit displays an error

"Key 'userchoice' not found in <QueryDict: {}>"

Pagination limits data to display properly, but when I click "next" it displays an error.

Here's what I've got so far:

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')

in custom templates :-

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

Pagination in Django works fine, it's your code that's the problem.

For some reason, you're using POST to send the original search variables, but then creating pagination links that just do GET with a page number. Of course, Django has no way of knowing what your previous search criteria are, since you're not sending them in the POST data - hence the error.

The normal way to do this is to send the original search request via GET - that is best practice anyway, since a search does not modify data. Then you include those same variables on all the pagination links, simply replacing the page number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM