Pass arguments using the HttpResponseRedirect in Django

user782400 picture user782400 · Apr 16, 2014 · Viewed 9.8k times · Source

I am trying to pass some information to my index page(index.html) from views.py. One passed, I need to access it in the index page. I have tried googling it, but it wasn't clear to me. Any help would be appreciated. I have attached below what I have tried.

Below I want to access the value "bar" in the index page. How can I do that ?

def tempLogin(request):
   username = request.POST['username']
   password = request.POST['password']
   user = authenticate(username=username, password=password)
   if user is not None:
       if user.is_active:
           login(request, user)
           return HttpResponseRedirect(reverse('index',foo='bar'))
       else:
           return HttpResponseRedirect(reverse('index'))
   else:
       return HttpResponseRedirect(reverse('index'))

Answer

Daniel Roseman picture Daniel Roseman · Apr 16, 2014

I see this question quite a lot, and it betrays a lack of understanding of what HttpResponseRedirect does. All it does is tell the browser to go and fetch another URL: so the only parameters you can pass to it are those that would be expected by that other URL pattern. If your index URL has an optional space for you to put a name value, then you can pass it. Otherwise, you'll need to do it some other way: perhaps in the session.