Django - Paginating in Template using multiple GET parameters

房东的猫 提交于 2021-02-18 15:40:21

问题


I am using a Django Paginator and I want to have multiple available get parameters, such as: page=1 sort_by=price

However, in my template tags I have:

Showing items sorted by {{ SORT_PARAM }}.
Showing {{ ITEMS_PER_PAGE }} items per page.

{% if has_prev %}
<a href="?page={{ prev_page }}">Previous</a> |
{% endif %}

However, this does not preserve the other GET variables. What I mean is, if I'm viewing

page/?page=1&sort_by=price

and I click the link in the template fragment above, I will go to

page=2

instead of

page=2&sort_by=price

What I mean is, the a href does not preserve the other GET parameters.

One solution is I could type all the possible GET parameters in the a href, such as

<a href="?page={{ prev_page }}&items_per_page={{ ITEMS_PER_PAGE }}&sort_param={{ SORT_PARAM }}">Previous</a>

but this will become less scalable the more arguments I want to add to my browsing. I'm guessing there should be an automated way to obtain all GET parameters, and then pass those and one more?


回答1:


You can create a 'parameter-string'. Let's supose that in your code you have:

my_view( request, page, options):
    sort_choices = {P:'price',N:'name', ...}
    n_item_choices = {'S':5, 'L':50, 'XL':100)
    ascending_descending_choices = {'A':'', 'D':'-'}
    ...

then you can concatenat options as:

options='P-S-D'  #order by price, 5 items per page, descending order

encode opions as:

<a href="?page={{ prev_page }}&options={{ options }}">Previous</a>

then, in urls.py capture options and in view:

my_view( request, page, options):
   ... #choides ....
   try:
      optionsArray = options.split('-')
      sort_by = sort_choices[ optionsArray[0]  ]
      n_ites_page = n_item_choices[ optionsArray[1]  ]
      asc_or_desc = ascending_descending_choices[ optionsArray[2]  ]
      ...
   except:
      somebody is playing ....

with this method you are free to add more paginations options without modify urls.py, all you need is to append options at the end of string options . This has advantages but also some dangers: I hope you can identify risks.




回答2:


This one http://djangosnippets.org/snippets/1592/ looks cleaner




回答3:


With Django's Pagination - preserving the GET params is simple.

First copy the GET params to a variable (in view):

GET_params = request.GET.copy()

and send it to the template in via context dictionary:

return render_to_response(template,
                        {'request': request, 'contact': contact, 'GET_params':GET_params}, context_instance=RequestContext(request))

Second thing you need to do is use it specify it in the url calls (href) in the template - an example (extending the basic pagination html to handle extra param condition):

{% if contacts.has_next %}
    {% if GET_params %}
        <a href="?{{GET_params.urlencode}}&amp;page={{ contacts.next_page_number }}">next</a>
    {% else %}
        <a href="?page={{ contacts.next_page_number }}">next</a>
    {% endif %}
{% endif %}

Source - Posted same answer.



来源:https://stackoverflow.com/questions/8386191/django-paginating-in-template-using-multiple-get-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!