问题
In Python/Django I have a FormView that allows you to add certain field choices ad hoc.
(Example: Selecting a soda from the soda dropdown)
Select Favorite soda: [_Soda_Choices_Dropdown_] +Add soda
I want to be able to add a soda on the fly, and when I save the soda I want the success URL to be the page that brought you there.
[Page 1] --> [Create new soda FormView] --Success-> [Page 1]
What would be the best way to accomplish this?
Thanks!
回答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.
<a href='/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
.
<form method="POST" action="/my/soda/form/page/?next={{next_url}}">
Your view would look something like:
class MySodaFormView(FormView):
def get_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
def get_success_url(self):
next_url = self.request.GET.get('next')
if next_url:
return next_url # return next url for redirection
return 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
class MySodaFormView(FormView):
def get_success_url(self):
referer_url = self.request.META.get('HTTP_REFERER') # get the referer url from request's 'META' dictionary
if referer_url:
return referer_url # return referer url for redirection
return 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.
回答2:
def soda_view(request):
# your code goes here
url = "{0}/{1}".format(request.META.get('HTTP_REFERER', '/'), args)
return HttpResponseRedirect(url)
回答3:
FormView, along with other generic class-based views that have FormMixin, has a method get_success_url()
that you can use to return to the same page. It would look something like this:
from django.core.urlresolvers import reverse
def get_success_url(self):
return reverse('url_name_of_page_1')
Or, to combine this with Geo Jacob's answer, get the referring URL out of the HTTP headers:
def get_success_url(self):
if 'HTTP_REFERER' in request.META:
return request.META['HTTP_REFERER']
else:
# Do something with the error case
回答4:
As you are using FormView
, simply do this:
from django.shortcuts import reverse
class YourView(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.
来源:https://stackoverflow.com/questions/31766235/returning-to-page-that-brought-you-there-django