金字塔中的POST后如何重定向? [英] How can I redirect after POST in Pyramid?

查看:82
本文介绍了金字塔中的POST后如何重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将表单提交到路由,该路由将验证数据,然后重定向回原始路由.

I'm trying to have my form submit to a route which will validate the data then redirect back to the original route.

例如:

  • 用户加载网页website.com/post
  • 表格将数据发布到website.com/post-save
  • 用户被重定向回website.com/post

金字塔给我带来了一些麻烦.

Pyramid is giving me some troubles doing this.

这是我瘦下来的views.py

Here's my slimmed down views.py

def _get_link_form(post_data):
    """ Returns the initialised form object """

    return LinkForm(post_data)

def home_page(request):

    form = _get_link_form(request.POST)
    return {'form' : form}

def save_post(request):
    """ form data is submitted here """"

    form = _get_link_form(request.POST)

    if not form.validate():
        return home_page(request, form)

这是我一直在玩的代码.它不仅不起作用,而且还感到混乱和被黑客入侵.当然,在金字塔中有一种更简单的方法来重定向POST后"吗?

This is the code I've been playing around with. Not only does it not work, it also feels messy and hacked up. Surely there's a simpler way to 'redirect after-POST' in Pyramid?

推荐答案

通过简单地POST到显示表单的相同URL,然后在POST时将用户重定向到页面之外,最容易解决您的问题.成功的.这样,在成功提交表单之前,您不会更改URL.

Your problem is most easily solved by simply POSTing to the same URL that your form is shown at, and simply redirecting the user away from the page when the POST is successful. That way until the form is successfully submitted you do not change URLs.

如果您只是想将POST发送到其他URL,那么您显然需要使用会话保存数据,因为您显然在处理请求之间的表单数据.

If you're just dying to POST to a different URL, then you need to save the data using sessions, since you're obviously handling the form data between requests.

通常,如果您希望能够处理表单中的错误,则可以使用会话和Flash消息.为此,您只需添加一个位置即可使Flash消息出现在基本模板中,并使用pyramid_beaker之类的设置来设置会话支持.

Typically if you want to be able to handle errors in your forms you would use a session and flash messages. To do this you simply add a location for flash messages to appear in your base template and setup session support using something like pyramid_beaker.

假设您的首页已设置在首页"命名路线上:

Assuming your home page is setup at the 'home' named-route:

from pyramid.httpexceptions import HTTPFound

def myview(request):
    user = '<default user field value>'
    if 'submit' in request.POST:
        user = request.POST.get('user')
        # validate your form data
        if <form validates successfully>:
            request.session.flash('Form was submitted successfully.')

            url = request.route_url('home') 
            return HTTPFound(location=url)
    return {
        # globals for rendering your form
        'user': user,
    }

请注意,如果表单未能通过验证,您如何使用与原始呈现表单相同的代码,并且只有在成功时才进行重定向.这种格式还可以处理使用提交中使用的值和默认值填充表单.

Notice how if the form fails to validate you use the same code you did to render the form originally, and only if it is successful do you redirect. This format can also handle populating the form with the values used in the submission, and default values.

您可以使用request.session.peek_flash()request.session.pop_flash()在所选模板中循环浏览Flash消息.

You can loop through the flash messages in your template of choice using request.session.peek_flash() and request.session.pop_flash().

route_url还支持对生成的url上的查询字符串进行突变.

route_url supports mutating the query string on the generated url as well, if you want to flag your home page view to check the session data.

您显然可以将查询字符串中的所有内容都传递回首页,但这是一个很大的安全漏洞,会话可以帮助防止该漏洞.

You can obviously just pass everything in the query string back to the home page, but that's a pretty big security vulnerability that sessions can help protect against.

这篇关于金字塔中的POST后如何重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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