52
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)
65
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.urls import reverse
url = reverse('notamember', kwargs={'classname': classname})
return HttpResponseRedirect(url)
- [Django]-GeoDjango GEOSException error
- [Django]-How can I test binary file uploading with django-rest-framework's test client?
- [Django]-Django F expressions joined field
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)
- [Django]-How to handle request.GET with multiple variables for the same parameter in Django
- [Django]-POST jQuery array to Django
- [Django]-Get request data in Django form
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
- [Django]-Override a form in Django admin
- [Django]-Separation of business logic and data access in django
- [Django]-How do I migrate a model out of one django app and into a new one?
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' }
)
- [Django]-How to set ForeignKey in CreateView?
- [Django]-Django: Create fixtures without specifying a primary key?
- [Django]-SyntaxError: Generator expression must be parenthezised / python manage.py migrate