Monday, February 27, 2017

What's the best way to create a reusable pagination template?

The code for rendering pagination controls in my app is the same everywhere:

{% if is_paginated %}
   
<div class="row">
       
<div class="col xs-12">
           
{% if page_obj.has_previous %}
               
<a class="btn btn-primary" href="foo/?page={{ page_obj.previous_page_number }}">Previous</a>
            {%
endif %}
            {%
if page_obj.has_next %}
                <
a class="btn btn-primary"
                   href="foo/?page={{ page_obj.next_page_number }}">Previous</a>
            {%
endif %}
        </
div>
    </
div>
{%
endif %}

So I'm wondering whether there's a good w&ay to create the href for the previous and next buttons based on the current url. I can't just take the current url and append "?page=..." to it, since the url may already have a "page" parameter and/or other querystring parameters. On stackoverflow, I've seen people create a replace_url_parameter template tag, which replaces the given parameter by parsing the url, changing the param, and rebuilding the url again. I can do this, but this seems like such a common task that I'm surprised I can't find any tools to do this in django itself.

If there's an open source project that offers this, I might be interested, but I'm not sure if I want to take on an extra dependency just for pagination urls.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4cf546e8-a01b-4cea-8f42-92a5dcb0ba3a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment