Python 回到那个把你们带到那个里的页面Django
在Python/Django中,我有一个FormView,允许您临时添加某些字段选择 (示例:从“汽水”下拉列表中选择汽水) 选择喜爱的苏打水:[[u苏打水\u选项\u下拉菜单]+添加苏打水 我希望能够在运行中添加苏打水,并且当我保存苏打水时,我希望成功URL成为将您带到那里的页面 [Page 1]-->[Create new soda FormView]--Success->[Page 1] 实现这一目标的最佳方式是什么Python 回到那个把你们带到那个里的页面Django,python,django,formview,Python,Django,Formview,在Python/Django中,我有一个FormView,允许您临时添加某些字段选择 (示例:从“汽水”下拉列表中选择汽水) 选择喜爱的苏打水:[[u苏打水\u选项\u下拉菜单]+添加苏打水 我希望能够在运行中添加苏打水,并且当我保存苏打水时,我希望成功URL成为将您带到那里的页面 [Page 1]-->[Create new soda FormView]--Success->[Page 1] 实现这一目标的最佳方式是什么 谢谢 FormView,以及其他具有FormMixin的基于类的通用视图
谢谢 FormView,以及其他具有FormMixin的基于类的通用视图,有一个方法
get\u success\u url()
,可用于返回同一页面。它看起来像这样:
def soda_view(request):
# your code goes here
url = "{0}/{1}".format(request.META.get('HTTP_REFERER', '/'), args)
return HttpResponseRedirect(url)
from django.core.urlresolvers import reverse
def get_success_url(self):
return reverse('url_name_of_page_1')
或者,要结合Geo Jacob的回答,请从HTTP头中获取引用URL:
def get_success_url(self):
if 'HTTP_REFERER' in request.META:
return request.META['HTTP_REFERER']
else:
# Do something with the error case
编辑: 最好在请求中使用
next
参数,以重定向到为我们购买表单页面的页面,而不是使用HTTP\u REFERER
假设您在页面some_page.html
上有一个指向MySodaFormView
页面的链接。在这里,将request.path
作为next
参数传递。这将在重定向时使用
<a href='/my/soda/form/page/?next={{request.path}}'>Create new soda</a>
您的视图类似于:
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
编辑:以下使用HTTP\u REFERER
的方法有时可能不起作用,因为某些浏览器关闭了passing REFERER功能,或者为用户提供了禁用该功能的选项。
要返回为您购买的页面,您可以使用字典中的HTTP\u REFERER
标题
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
HttpRequest.META
是包含所有可用HTTP头的标准Python字典。其中一个标题是HTTP\u REFERER
,其中包含引用页面(如果有)
由于您使用的是FormView
,因此可以覆盖get\u success\u url()
函数,以便在成功时重定向到用户购买到MySodaFormView
的页面。我们将使用request.META
字典中的HTTP\u REFERER
值获取此页面
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
注意:使用
request.META
字典中的HTTP\u REFERER
可能不是“最佳实践”,因为某些浏览器关闭了passing REFERER功能,或者为用户提供了禁用该功能的选项。在这种情况下,重定向将无法正常工作。您可以在url中传递一个?next=
参数,并在get\u success\u url()
函数中,使用next
的值来获取要重定向到的url。在使用FormView
时,只需执行以下操作:
from django.shortcuts import reverse
class YourView(FormView):
success_url = reverse('first-page')
在您的url.py
中:
url(r'/foo/bar/', some.view, name='first-page'),
first page
是在图表中呈现page 1
的视图的名称 一般来说,没有解释的答案对未来的观众来说是相对无用的。请在你的回答中加入解释。请详细说明<代码>第1页不是固定页面。根据问题,用户可以从多个页面访问MySodaFormView
页面。您指的是用户到达表单,填写信息然后提交的情况吗。HTTP\u REFERER是表单页面的URL,而不是用户最初来自的页面的URL