how to redirect to a url with query string django

the answer is pretty simple. Using reverse and passing name of url can redirect to url with query string

urls.py

url(r'^search/$', views.search, name='search_view')

views.py

from django.shortcuts import redirect, reverse

# in method
return redirect(reverse('search_view') + '?item=4')

I know this question is a bit old, but someone will stumble upon this while searching redirect with query string, so here is my solution:

import urllib
from django.shortcuts import redirect

def redirect_params(url, params=None):
    response = redirect(url)
    if params:
        query_string = urllib.urlencode(params)
        response['Location'] += '?' + query_string
    return response

def your_view(request):
    your_params = {
        'item': 4
    }
    return redirect_params('search_view', your_params)

Tags:

Python

Django