URL Query Params via Reverse.

296 views
Skip to first unread message

Matthew Bridges

unread,
Mar 5, 2018, 7:32:43 AM3/5/18
to Django developers (Contributions to Django itself)
Hello all, first contribution to the dev community here, I wanted to get some thoughts on a feature that I thought might be a helpful.

I notice myself writing this pattern a lot when constructing redirect URLS.

```
def some_view(request):
     querystring = "?some-key={some_key}&another-key={another_key}".format(some_key=some_value, another_key=another_value)
     url = "{url}{querystring}".format(url=reverse('some_app:some_view'), querystring=querystring)
     return HttpResponseRedirect(url)
```

I was thinking about writing a helper function that grabbed a reversed URL, and unpacked a data dict into a query string. Building up strings feels nasty, and doing this in a functional manner feels much nicer.

Pseudo code might look something like:

```
    def reverse_with_querystring(viewname, data=None)
        # get viewname

        # loop over data

        # IRI encode url

        # return constructed URL
```

```
    url = reverse_with_querystring('myapp:myview', data={'keyone': '123', 'keytwo' 321)
```

Questions:
 - Do other people use this syntax to build up querystrings / am I solving a problem that doesn't need solving?
 - Is there already a helper that allows this behaviour?
 - If the function were to be included, would django/urls be the appropriate module, given that the function utilises reverse? Or perhaps somewhere else. The behaviour would be similar to get_absolute_url type functions.
 - Am I doing this right?

Thanks :)

Collin Anderson

unread,
Mar 5, 2018, 8:52:14 AM3/5/18
to django-d...@googlegroups.com
Hi Matthew,


It sounds like you want something like:
def reverse_with_querystring(viewname, data=None):
    return "{url}{querystring}".format(url=reverse(viewname), querystring='?' + urlencode(data) if data else '')

Collin

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscribe@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/7846f70a-ddf9-47fe-b99f-915d2ff7ce48%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tim Graham

unread,
Mar 5, 2018, 11:00:17 AM3/5/18
to Django developers (Contributions to Django itself)
The problem is discussed in https://code.djangoproject.com/ticket/25582. I knew the ticket existed but I found it with the Google search: site:code.djangoproject.com reverse querystring.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages