URL Query Params via Reverse.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

URL Query Params via Reverse.

Matthew Bridges
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 :)

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: URL Query Params via Reverse.

Collin Anderson-2
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

On Mon, Mar 5, 2018 at 5:35 AM, Matthew Bridges <[hidden email]> wrote:
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 :)

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/CAFO84S6gUxTurNqD5KgqP_k7O5UWfddceAjWN8ujuq9AJ%3DJG%3DQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: URL Query Params via Reverse.

Tim Graham-2
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.

On Monday, March 5, 2018 at 8:52:14 AM UTC-5, Collin Anderson wrote:
Hi Matthew,

Are you aware of Python's urlencode? <a href="https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlencode" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.python.org%2F3%2Flibrary%2Furllib.parse.html%23urllib.parse.urlencode\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEUvdlf6lYl7PYnG-HqvYjEJTF1MA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.python.org%2F3%2Flibrary%2Furllib.parse.html%23urllib.parse.urlencode\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEUvdlf6lYl7PYnG-HqvYjEJTF1MA&#39;;return true;">https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlencode

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

On Mon, Mar 5, 2018 at 5:35 AM, Matthew Bridges <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="oHcDXHpHBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">matthew...@...> wrote:
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 :)

--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="oHcDXHpHBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django-develop...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="oHcDXHpHBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django-d...@googlegroups.com.
Visit this group at <a href="https://groups.google.com/group/django-developers" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/django-developers&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/django-developers&#39;;return true;">https://groups.google.com/group/django-developers.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/django-developers/7846f70a-ddf9-47fe-b99f-915d2ff7ce48%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/django-developers/7846f70a-ddf9-47fe-b99f-915d2ff7ce48%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/django-developers/7846f70a-ddf9-47fe-b99f-915d2ff7ce48%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/django-developers/7846f70a-ddf9-47fe-b99f-915d2ff7ce48%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/6d2efe14-05c4-4c9a-9320-7e903818ff0f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.