Pylons FormEncode @validate 装饰器将参数传递到重新渲染操作中
我正在尝试将 Pylons 中的验证装饰器与 FormEncode 结合使用,但遇到了问题。我正在尝试验证需要参数的控制器操作上的表单,如果验证失败,则重新呈现表单时不会传回参数。这是一个例子。
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 标识。我的添加问题控制器操作如下所示:
@validate(schema=QuestionForm(), form='question_set', post_only=True)
def add_question(self):
stuff...
现在,如果验证失败,FormEncode 会尝试重新显示 Question_set 表单,但它不会将 id 参数传回,因此问题集表单将不会呈现。是否可以使用 @validate 装饰器将 id 传回,或者我是否需要使用不同的方法来实现我想要做的事情?
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')
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...
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题是
add_question()
没有收到id
参数。尝试设置您的路由,以便add_question()
不仅在 POST 变量中接收它,而且还作为参数接收它,看看它是否解决了问题。I think the problem is that
add_question()
doesn't receiveid
argument. Try to set up your routing so thatadd_question()
receives it not only in POST vars but also as argument and see if it fixes the issue.我有类似的问题。我调整了我的路线以包含 id 并且它有效。
I had a similar issue. I adjusted my route to include the id and it worked.