37

I have a situation in my project where i need to make a redirection of the user to an url containing a parameter, it is declared in the urls.py like:

url(r'^notamember/(?P<classname>\w+)/$', 
                           notamember,
                           name='notamember'),)

How can i put that parameter in the return HttpResponseRedirect? I tried like:

return HttpResponseRedirect('/classroom/notamember/classname')

anyway, this is foolish, i know, i cannot consider the classmane as a parameter. For clarity, my view.py is:

def leave_classroom(request,classname):
    theclass = Classroom.objects.get(classname = classname)
    u = Membership.objects.filter(classroom=theclass).get(member = request.user).delete()
    return HttpResponseRedirect('/classroom/notamember/theclass/')

How can i include the variable theclass in that url? Thanks a lot!

Pradip
  • 1,204
  • 3
  • 18
dana
  • 4,390
  • 19
  • 70
  • 115

5 Answers5

59

This should not be complicated. The argument to HttpResponseRedirect is simply a string, so the normal rules for building up a string apply here. However, I don't think you want the theclass variable in there, as that is a ClassRoom object, not a string. You presumably want the classname instead. adamk has given you the right answer here.

However, having said that you can just use a string, what you should actually do is use the reverse function. This is because you might later decide to change the URL structure, and rather than having to look through your code finding each place you've hard-coded the URL string, you should rely on having defined them in one single place: your urls.py file. So you should do something like this:

from django.core.urlresolvers import reverse

url = reverse('notamember', kwargs={'classname': classname})
return HttpResponseRedirect(url)
Daniel Roseman
  • 541,889
  • 55
  • 754
  • 786
49

Try this:

return HttpResponseRedirect('/classroom/notamember/%s/' % classname)

EDIT:

This is surely better (Daniel Roseman's answer):

from django.core.urlresolvers import reverse

url = reverse('notamember', kwargs={'classname': classname})
return HttpResponseRedirect(url)
adamk
  • 38,100
  • 6
  • 47
  • 56
  • hmmm... it doesn't seem to work - the classname still doesn't appear in the url :( – dana Jun 29 '10 at 11:11
  • what does the `notamember` view do ? – Ashok Jun 29 '10 at 11:18
  • 1
    gives the user access only to the resources available for non members. its signature is def notamember(request,classname): – dana Jun 29 '10 at 11:20
  • From the urls.py you included I understand that you want that when the user goes to 127.0.0.1:8000/notamember/myclass/, the browser will redirect to 127.0.0.1:8000/classroom/notamember/myclass/? – adamk Jun 29 '10 at 11:27
  • 4
    Daniel's answer is a much better solution than this one. The url in urls.py may change at a later time. With this method you'll have to find all the places where it occurs and change accordingly, whereas if you use reverse() that will happen automatically. – rz. Jun 29 '10 at 21:45
5

Actually, the shortcut redirect takes view names and model (which has get_absolute_url defined) names too.

from django.shortcuts import redirect

return redirect(leave_classroom)
Lakshman Prasad
  • 76,135
  • 46
  • 128
  • 164
0

When everything seems not to be working i use return render and check if it is post request in case anybody refresh the page

if request.POST:
    message = "Thank you."
    return render(request, 'index.html', locals())
return HttpResponseRedirect('/')

The local() make the parameter accessible on the template

Ahmed Adewale
  • 2,090
  • 17
  • 12
0

If you are submitting to the same URL, you can use the following to pass parameters.

 template_name = '/classroom/notamember.html'

return render(
                request,
                self.template_name,
                {'classname': 'classname', 'secondvariable': 'variable' }
            )
Haris Np
  • 607
  • 7
  • 14