How does one redirect from one View to another (next-going one):
class FooView(TemplateView):
template_name 'foo.html'
def post(self, *args, **kwargs):
return redirect(BarView)
# return redirect(BarView.as_view()) ???
class BarView(TemplateView):
template_name 'bar.html'
Permanent redirects are for when resource URLs change.
The render function Combines a given template with a given context dictionary and returns an HttpResponse object with that rendered text. You request a page and the render function returns it. The redirect function sends another request to the given url.
the actual reverse function is used for performing the reverse happen. This method will be responsible for getting the new url value reversed. With the reverse function the name of the redirect url has to be specified. The name of url value specified here will for the redirect url name from the url's.py file.
Give the URL pattern itself a name in your urls.py:
url('/bar/', BarView.as_view(), name='bar')
and just pass it to redirect:
return redirect('bar')
You can use the redirect for that, if you've given the view a name in urls.py
.
from django.shortcuts import redirect
return redirect('some-view-name')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With