Append a querystring to URL in django

Pass the query string in the template:

<a href="{% url 'my_videos' %}?filter=others&sort=newest">My Videos</a>

I'm not sure whether this was possible at the time of this question being asked, but if you have the data available to build the query string in your view and pass it as context, you can make use of the QueryDict class. After constructing it, you can just call its urlencode function to make the string.

Example:

from django.http import QueryDict

def my_view(request, ...):
    ...
    q = QueryDict(mutable=True)
    q['filter'] = 'other'
    q['sort'] = 'newest'
    q.setlist('values', [1, 2, 'c'])
    query_string = q.urlencode()
    ...

query_string will now have the value u'filter=other&sort=newest&values=1&values=2&values=c'


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


if my_videos:
    return HttpResponseRedirect( reverse('my_videos') + "?filter=others&sort=newest")

Tags:

Django