#10941: Add a templatetag to generate querystrings
---------------------------------+------------------------------------
     Reporter:  Ben Spaulding    |                    Owner:  (none)
         Type:  New feature      |                   Status:  new
    Component:  Template system  |                  Version:  dev
     Severity:  Normal           |               Resolution:
     Keywords:  pagination       |             Triage Stage:  Accepted
    Has patch:  0                |      Needs documentation:  0
  Needs tests:  0                |  Patch needs improvement:  0
Easy pickings:  0                |                    UI/UX:  0
---------------------------------+------------------------------------
Changes (by Carsten Fuchs):

 * cc: Carsten Fuchs (added)


Comment:

 I too had reinvented this wheel a couple of years ago, unaware of this
 ticket. Other implementations above are more powerful, but this one is
 short:
 {{{
 #!python
 @register.simple_tag
 def query_string(*args, **kwargs):
     """
     Combines dictionaries of query parameters and individual query
 parameters
     and builds an encoded URL query string from the result.
     """
     query_dict = QueryDict(mutable=True)

     for a in args:
         query_dict.update(a)

     remove_keys = []

     for k, v in kwargs.items():
         if v is None:
             remove_keys.append(k)
         elif isinstance(v, list):
             query_dict.setlist(k, v)
         else:
             query_dict[k] = v

     for k in remove_keys:
         if k in query_dict:
             del query_dict[k]

     qs = query_dict.urlencode()
     if not qs:
         return ""
     return "?" + qs
 }}}

 It cannot do everything, but covers all my use cases: Keep most of the
 existing source parameters, but delete, add or change some. Examples:
 {{{
 {# Just repeat the parameters: #}
 {% query_string request.GET %}

 {# Add a parameter: #}
 {% query_string request.GET format='pdf' %}

 {# Change a parameter: #}
 {% query_string request.GET page=next_page_number %}

 {# Overwrite month and year with precomputed values, e.g. with
 next_month_year = {'month': 1, 'year': 2022}, and clear the day: #}
 {% query_string request.GET next_month_year day=None %}
 }}}

 I'm aware that posting yet another implementation does not achieve true
 progress and that the more elaborate implementations above may be more
 appropriate for inclusion in Django.
 Still, I wanted to mention this because in my opinion it keeps the balance
 between features and simplicity.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/10941#comment:33>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/070.d2273be992549f011746ef202b3d403f%40djangoproject.com.

Reply via email to