29

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?

Community
  • 1
  • 1
Saad Abdullah
  • 2,252
  • 3
  • 29
  • 42

5 Answers5

48

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')
Matthew Schinckel
  • 35,041
  • 6
  • 86
  • 121
Saad Abdullah
  • 2,252
  • 3
  • 29
  • 42
  • 2
    This should probably not be the accepted answer. You should use `urlencode` as in [this answer](https://stackoverflow.com/a/41875812/3393459) – exp1orer May 14 '21 at 02:04
  • If you set the `Location` header manually that's true but if you use `HttpResponseRedirect` or the `redirect` shortcut, it [calls iri_to_uri](https://github.com/django/django/blob/4.2.4/django/http/response.py#L616) which [calls urlencode for you](https://github.com/django/django/blob/4.2.4/django/utils/encoding.py#L138) 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. – Ryan Nowakowski Aug 13 '23 at 21:50
15

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)
fixmycode
  • 8,220
  • 2
  • 28
  • 43
  • Warning! urllib has been split up in python3. Check [this](https://stackoverflow.com/a/28906913/9395299). Nevertheless, this solution works very well. Thank you – Maël Pedretti Jul 20 '22 at 05:32
5

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()
    )
Matthew Schinckel
  • 35,041
  • 6
  • 86
  • 121
  • 1
    Note it's also possible to create your own `QueryDict` instance, see e.g. https://stackoverflow.com/a/15010861 – djvg Dec 22 '22 at 11:21
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)

karuhanga
  • 3,010
  • 1
  • 27
  • 30
0

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)
ruohola
  • 21,987
  • 6
  • 62
  • 97
adigunsherif
  • 73
  • 1
  • 7