Pagination in django - the original query string gets lost

前端 未结 4 851
慢半拍i
慢半拍i 2021-01-12 02:23

I use the code from the documentation to paginate the data:

try:
    data = paginator.page(request.GET.get(\'page\'))
except PageNotAnInteger:
    page = 1
          


        
4条回答
  •  太阳男子
    2021-01-12 03:14

    Another possible solution can be to construct parameters list in your view. Pros: you can use clean and expressive methods on QueryDict.

    It will be look like this:

    get_copy = request.GET.copy()
    parameters = get_copy.pop('page', True) and get_copy.urlencode()
    context['parameters'] = parameters
    

    That's it! Now you can use your context variable in template:

     href="?page={{ paginator.next_page_number }}&{{ parameters }}" 
    

    See, code looks clean and nicely.

    note: assumes, that your context contained in context dict and your paginator in paginator variable

提交回复
热议问题