Returning To Page That Brought You There Django
Solution 1:
EDIT:
It would be better to use next
parameter in the request to redirect to the page that bought us to form page instead of using HTTP_REFERER
.
Lets say you are on page some_page.html
having a link to the MySodaFormView
page. Here, pass the request.path
as a next
parameter. This will be used when redirecting.
<ahref='/my/soda/form/page/?next={{request.path}}'>Create new soda</a>
Then in MySodaFormView
when rendering the page, pass the next
parameter in the context. This parameter will be passed in form
action and will be used when redirecting.
In your soda formview template, specify next
parameter in the form action
.
<formmethod="POST"action="/my/soda/form/page/?next={{next_url}}">
Your view would look something like:
classMySodaFormView(FormView):defget_context_data(self, **kwargs):
context = super(MySodaFormView, self).get_context_data(**kwargs)
context['next_url'] = self.request.GET.get('next') # pass `next` parameter received from previous page to the context return context
defget_success_url(self):
next_url = self.request.GET.get('next')
ifnext_url:return next_url # return next url for redirectionreturn other_url # return some other url if next parameter not present
EDIT: The below approach using HTTP_REFERER
might not work sometimes as some browsers have passing referer feature turned off or provide the user an option to disable that feature.
To return to the page that bought you there, you can use HTTP_REFERER
header present in the HttpRequest.META
dictionary.
HttpRequest.META
is a standard Python dictionary containing all available HTTP headers. One of the headers among them is HTTP_REFERER
which contains the referring page if any.
Since you are using FormView
, you can override the get_success_url()
function to redirect on success to the page which bought the user to MySodaFormView
. We will get this page using the value of HTTP_REFERER
in the request.META
dictionary.
from django.views.generic.edit import FormView
classMySodaFormView(FormView):
defget_success_url(self):
referer_url = self.request.META.get('HTTP_REFERER') # get the referer url from request's 'META' dictionaryif referer_url:
return referer_url # return referer url for redirectionreturn other_url # return some other url if referer url not present
Note: Using HTTP_REFERER
from request.META
dictionary may not be a "best practice" as some browsers have passing referer feature turned off or provide the user an option to disable that feature. In that case, your redirection would not work properly. You could instead pass a ?next=
parameter in the url and in your get_success_url()
function, use the value of next
to get the url to redirect to.
Solution 2:
defsoda_view(request):
# your code goes here
url = "{0}/{1}".format(request.META.get('HTTP_REFERER', '/'), args)
return HttpResponseRedirect(url)
Solution 3:
As you are using FormView
, simply do this:
from django.shortcuts import reverse
classYourView(FormView):
success_url = reverse('first-page')
In your urls.py
:
url(r'/foo/bar/', some.view, name='first-page'),
first-page
being the name of the view that renders Page 1
in your diagram.
Post a Comment for "Returning To Page That Brought You There Django"