35

How can I go to a specific URL with parameters like if I have view

def search(request):

and in urls.py

^search/$ 

and what I need to do is to redirect like search/?item=4

c = {}
render_to_response("search.html",c) 

works fine, but

render_to_response("search.html/?item=" + itemID, c )

it says no template found ( I know there is no template like search.html/?item= ) but how can I pass parameters or use query string to redirect?

3
  • 1
    render_to_response has nothing to do with redirect! Commented Nov 5, 2013 at 8:56
  • I just want to redirect to some url like search/?item=5 (pass query string parameter) any other way? Commented Nov 5, 2013 at 9:05
  • docs.djangoproject.com/en/dev/topics/http/shortcuts/#redirect Commented Nov 5, 2013 at 9:13

5 Answers 5

57

Using reverse and passing the name of url we 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')
Sign up to request clarification or add additional context in comments.

2 Comments

This should probably not be the accepted answer. You should use urlencode as in this answer
If you set the Location header manually that's true but if you use HttpResponseRedirect or the redirect shortcut, it calls iri_to_uri which calls urlencode for you so you don't need to do it yourself. If fact, if you do it yourself, the URL will get double encoded which mangles it.
16

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)

1 Comment

Warning! urllib has been split up in python3. Check this. Nevertheless, this solution works very well. Thank you
6

If you already have a request (with a GET containing some parameters), and you want to redirect to a different view with the same args, kwargs and parameters, we can use the QueryDict object to url encode itself:

def view(request, *args, **kwargs):
    return redirect(
        reverse('your:view:path', args=args, kwargs=kwargs) + 
        '?' + request.GET.urlencode()
    )

1 Comment

Note it's also possible to create your own QueryDict instance, see e.g. stackoverflow.com/a/15010861
2

A more generic option;

from urllib.parse import urlencode
from django.shortcuts import redirect as django_redirect


def redirect(url, *args, params=None, **kwargs):
    query_params = ""
    if params:
        query_params += '?' + urlencode(params)
    return django_redirect(url+query_params, *args, **kwargs)

Comments

1

To redirect to another page while carrying along the the current query strings:

views.py:

from django.urls import reverse
from django.shortcuts import redirect

def my_view(request):
    #get the current query string
    q = request.META['QUERY_STRING']
    return redirect(reverse('search_view') + '?' + q)

2 Comments

Does not work if the search query string has escaped characters.
request.META['QUERY_STRING'] is allready escaped, works for me.

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.