Pylons FormEncode @validate 装饰器将参数传递到重新渲染操作中

发布于 2024-08-17 17:27:58 字数 724 浏览 5 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如若梦似彩虹 2024-08-24 17:27:58

我认为问题是 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.

嘦怹 2024-08-24 17:27:58

我有类似的问题。我调整了我的路线以包含 id 并且它有效。

I had a similar issue. I adjusted my route to include the id and it worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文