How do i pass GET parameters using django urlresolvers reverse

Solution 1:

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)

Solution 2:

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.

Solution 3:

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'