Django return HttpResponseRedirect to an url with a parameter Django return HttpResponseRedirect to an url with a parameter django django

Django return HttpResponseRedirect to an url with a parameter


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 reverseurl = reverse('notamember', kwargs={'classname': classname})return HttpResponseRedirect(url)


Try this:

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

EDIT:

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

from django.core.urlresolvers import reverseurl = reverse('notamember', kwargs={'classname': classname})return HttpResponseRedirect(url)


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

from django.shortcuts import redirectreturn redirect(leave_classroom)