如何在金字塔中POST后重定向?

时间:2011-04-10 06:06:39

标签: python post redirect pylons pyramid

我正在尝试让我的表单提交到一条路线,该路线将验证数据,然后重定向回原路线。

例如:

  • 用户加载页面website.com/post
  • 表格将数据发布到website.com/post-save
  • 用户被重定向回website.com/post
金字塔给我带来了一些麻烦。

这是我精简的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”吗?

6 个答案:

答案 0 :(得分:27)

只需POST到显示表单的同一URL,即可轻松解决问题,并在POST成功时将用户重定向到远离页面的位置。这样,在表单成功提交之前,您不会更改URL。

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

通常,如果您希望能够处理表单中的错误,则可以使用会话和Flash消息。要执行此操作,您只需使用pyramid_beaker等内容添加Flash消息的位置以显示在基本模板和设置会话支持中。

假设您的主页设置在'home'name-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,
    }

请注意,如果表单无法验证您是否使用了与最初呈现表单相同的代码,并且只有在成功时才重定向。此格式还可以处理使用提交中使用的值填充表单以及默认值。

您可以使用request.session.peek_flash()request.session.pop_flash()在您选择的模板中循环播放Flash消息。

如果您想标记主页视图以检查会话数据,

route_url也支持在生成的网址上改变查询字符串。

显然,您可以将查询字符串中的所有内容传递回主页,但这是一个非常大的安全漏洞,会话可以帮助防范。

答案 1 :(得分:8)

Pyramid文档有一个特别的点section,其中包含以下示例:

from pyramid.httpexceptions import HTTPFound

def myview(request):
    return HTTPFound(location='http://example.com')

答案 2 :(得分:1)

Pyramid文档包含有关 重定向 的内容,您可以在以下链接中看到更多信息:

Pyramid documentation

import pyramid.httpexceptions as exc
raise exc.HTTPFound(request.route_url("section1"))   # Redirect

答案 3 :(得分:0)

假设您的主页是金字塔网络应用的默认视图,您可以执行以下操作:

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 = _get_link_form(request.POST)

    if not form.validate():
        from pyramid.httpexceptions import HTTPFound
        return HTTPFound(location=request.application_url)

基本上你需要知道home_page视图是如何被添加的&#34;到您的配置器。如果您的主页实际上位于/ less / levels / deep / homepage,那么重定向可能如下所示:

        return HTTPFound(location=request.application_url + '/few/levels/deep/homepage')

答案 4 :(得分:0)

我是这样做的:

from pyramid.httpexceptions import HTTPCreated

response = HTTPCreated()
response.location = self.request.resource_url( newResource )
return response

这将发送HTTP Created代码,201

答案 5 :(得分:0)

一种干净的方式是使用金字塔为不同的请求类型提供的“重载”,例如,您可以这样装饰您的方法:

@action(request_method='GET',
        renderer='mypackage:/templates/save.mako',
        name='save')
def save(request):
    ''' Fill the template with default values or leave it blank'''
     return {}


@action(request_method='POST',
        renderer='mypackage:/templates/save.mako',
        name='save')
def save_post(request):
    """ form data is submitted here """"
    # process form

在HTML中,您必须调用操作表单,例如

<form method="POST" id="tform" action="${request.route_url('home', action='save')}">

这样,当使用方法POST时处理一个方法,而使用GET时处理另一个方法。相同的名称,但有两个实现。

相关问题