如何在最初呈现它的同一个控制器动作中验证塔中的表单?

时间:2010-09-18 09:27:28

标签: python pylons

我有以下控制器:

class FormtestController(BaseController):

    def form(self):
        return ender('/simpleform.html')

    @validate(schema=EmailForm(state=c), form='form', post_only=False, on_get=True,
              auto_error_formatter=custom_formatter)
    def submit(self):
        return 'Your email is: %s  and the date selected was %r.' % (
            self.form_result['email'],
            self.form_result['date'],
        )

第一个操作是初始表单呈现,第二个操作是表单提交时。是否可以合并它们,只需使用if request.POST =='POST'来检查表单是否已提交?

我尝试了它并将@validate装饰器移动到表单操作但它给了我一个WSOD并且服务器停止服务:

class FormtestController(BaseController):

    @validate(schema=EmailForm(state=c), form='form', post_only=False, on_get=True,
              auto_error_formatter=custom_formatter)
    def form(self):
        if request.method == 'POST':
            return 'Your email is: %s  and the date selected was %r.' % (
                self.form_result['email'],
                self.form_result['date'],
            )
        return render('/simpleform.html')

有没有办法让一个动作仍然使用验证装饰器?

1 个答案:

答案 0 :(得分:1)

傻了我,这很简单。这是我的代码:

class FormtestController(BaseController):

@validate(schema=EmailForm(state=c), form='form', post_only=True,
          on_get=False,
          auto_error_formatter=custom_formatter)
def form(self):
    if request.method == 'POST':
        return 'Your email is: %s  and the date selected was %r.' % (
            self.form_result['email'],
            self.form_result['date'],
        )
    return render('/simpleform.html')