92

I am using django 1.2 and going from one view to another using the urlresolvers reverse method.

url = reverse(viewOne)

and I want to pass a get parameter, for example

name = 'joe'

so that in the viewOne if I do

def viewOne(request):
    request.GET['name']

I will get

joe

how do I do that ?

2
  • 4
    Take a look at that answer that gives a rather nice way to do this kind of thing. Commented Sep 26, 2012 at 3:07
  • 1
    I created a feature request: code.djangoproject.com/ticket/25582 Commented Oct 21, 2015 at 9:06

4 Answers 4

164

GET parameters have nothing to do with the URL as returned by reverse. Just add it on at the end:

url = "%s?name=joe" % reverse(viewOne)
Sign up to request clarification or add additional context in comments.

1 Comment

This works for this example. Since no quoting gets done, this fails if you use values which need to be quoted.
84

A safer and more flexible way:

import urllib
from django.urls import reverse


def build_url(*args, **kwargs):
    get = kwargs.pop('get', {})
    url = reverse(*args, **kwargs)
    if get:
        url += '?' + urllib.urlencode(get)
    return url

then you can use build_url:

url = build_url('view-name', get={'name': 'joe'})

which takes same arguments as reverse, but provides an extra keyword argument get where you can put your GET parameters in it as a dictionary.

2 Comments

Note: Django's django.utils.http.urlencode function might be preferable since it is a "version of Python’s urllib.urlencode() function that can operate on unicode string": docs.djangoproject.com/en/1.10/ref/utils/…
I would rename this function to reverse_with_params() or something similar.
22

This is very similar to Amir's solution but handles lists as well.

from django.core.urlresolvers import reverse
from django.http import QueryDict

def build_url(*args, **kwargs):
    params = kwargs.pop('params', {})
    url = reverse(*args, **kwargs)
    if not params: return url

    qdict = QueryDict('', mutable=True)
    for k, v in params.iteritems():
        if type(v) is list: qdict.setlist(k, v)
        else: qdict[k] = v

    return url + '?' + qdict.urlencode()

Example usage:

>>> build_url('member-list', params={'format': 'html', 'sex': ['male', 'female']})
u'/members/?format=html&sex=male&sex=female'

2 Comments

What's the point of having sex=male&sex=female? I think it should be &sex[]=female&sex[]=female (note the additional []), to be a GET request array
@potatoes The [] are a PHP convention.
-2

Sorry for the delayed correction on this.

While both the answers here handles the required task too well, i think just a simple function to urlencode a dictionary is the most flexible way of doing this:

import urllib

def getify(dic):
    st = ''
    for K, V in dic.items():
        K = urllib.parse.quote(str(K))
        if isinstance(V, list):
            for v in V:
                st += K + '=' + urllib.parse.quote(str(v)) + '&'
        else:
            st += K + '=' + urllib.parse.quote(str(V)) + '&'
    return st.rstrip('&')

1 Comment

This does not urlencode the dictionary - there is no handling of URL reserved characters (e.g. #, ?) or any type of percent encoding: en.wikipedia.org/wiki/Percent-encoding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.