Pylons FormEncode @validate装饰器将参数传递到重新渲染操作中 [英] Pylons FormEncode @validate decorator pass parameters into re-render action

查看:119
本文介绍了Pylons FormEncode @validate装饰器将参数传递到重新渲染操作中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在带有FormEncode的Pylons中使用validate装饰器,但是遇到了问题。我正在尝试通过需要参数的控制器操作来验证表单,如果验证失败,则在重新呈现表单时不会将参数传递回去。这是一个示例。

I am attempting to use the validate decorator in Pylons with FormEncode and I have encountered an issue. I am attempting to validate a form on a controller action that requires parameters, and if the validation fails, the parameters aren't passed back in when the form is re-rendered. Here's an example.

def question_set(self, id):
    c.question_set = meta.Session.query(QuestionSet).filter_by(id=id).first()
    c.question_subjects = meta.Session.query(QuestionSubject).order_by(QuestionSubject.name).all()
    return render('/derived/admin/question_set.mako')

这是包含我的表单的控制器操作。表单会将问题添加到现有问题集,该问题集由ID标识。我的添加问题控制器操作如下所示:

This is the controller action that contains my form. The form will add questions to an existing question set, which is identified by id. My add question controller action looks like this:

@validate(schema=QuestionForm(), form='question_set', post_only=True)
def add_question(self):
    stuff...

现在,如果验证失败,则FormEncode会尝试重新显示问题集表单,但不会将id参数传递回,因此问题集表单将不会呈现。是否可以使用@validate装饰器将ID传递回去,还是我需要使用其他方法来实现我要尝试的操作?

Now, if the validation fails FormEncode attempts to redisplay the question_set form, but it does not pass the id parameter back in, so the question set form will not render. Is it possible to pass the id back in with the @validate decorator, or do I need to use a different method to achieve what I am attempting to do?

推荐答案

我认为问题是 add_question()没有收到 id 参数。尝试设置您的路由,以使 add_question()不仅可以在POST变量中接收它,而且还可以作为参数接收它,看看是否可以解决问题。

I think the problem is that add_question() doesn't receive id argument. Try to set up your routing so that add_question() receives it not only in POST vars but also as argument and see if it fixes the issue.

这篇关于Pylons FormEncode @validate装饰器将参数传递到重新渲染操作中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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