返回将您带到Django的页面 [英] Returning to page that brought you there Django

查看:102
本文介绍了返回将您带到Django的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python / Django中,我有一个FormView,可让您临时添加某些字段选择。

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)

选择喜欢的苏打水:[_Soda_Choices_Dropdown_] +添加苏打水

Select Favorite soda: [_Soda_Choices_Dropdown_] +Add soda

我希望能够即时添加苏打水,当我保存苏打水时,我希望成功URL是将您带到那里的页面。

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.

[第1页]-> [创建新的苏打FormView]-成功-> [第1页]

什么是最好的

谢谢!

推荐答案

编辑:

最好在使用 next 参数重定向到购买我们表单页面的请求,而不是使用 HTTP_REFERER

假设您在页面 some_page.html 上,具有指向 MySodaFormView 页。在这里,将 request.path 作为 next 参数传递。

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> 

然后在呈现页面时,在 MySodaFormView 中,在上下文中传递 next 参数。此参数将以 form 动作传递,并将在重定向时使用。

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.

在苏打水表单视图模板中,指定 next 参数的形式为 action

In your soda formview template, specify next parameter in the form action.

<form method="POST" action="/my/soda/form/page/?next={{next_url}}">

您的视图将类似于:

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_REFERER 有时可能无法工作,因为某些浏览器已关闭传递引用功能或为用户提供了禁用该功能的选项。

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.

返回到在那里购买您的页面,您可以使用HTTP_REFERER 标头。 http.HttpRequest.META rel = noreferrer> HttpRequest.META 字典。

To return to the page that bought you there, you can use HTTP_REFERER header present in the HttpRequest.META dictionary.

HttpRequest.META 是一个标准的Python字典,其中包含所有可用的HTTP标头。其中的标头之一是 HTTP_REFERER ,其中包含引用页面(如果有的话)。

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.

由于使用的是 FormView ,您可以覆盖 get_success_url()函数,以在成功重定向到将用户购买到<$ c $的页面c> MySodaFormView 。我们将使用 request.META 词典中的 HTTP_REFERER 的值来获取此页面。

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

注意:使用 request.META 词典中的 HTTP_REFERER 可能不是最佳实践,因为某些浏览器已关闭传递引用功能,或为用户提供了禁用该功能的选项。在这种情况下,您的重定向将无法正常工作。您可以改为在网址中传递?next = 参数,并在 get_success_url()函数中使用值下一步以获取重定向到的网址。

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.

这篇关于返回将您带到Django的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆