FormEncode 验证失败后使用查询字符串参数重新渲染 Pylons 表单
我的问题可能与此相同,但建议的答案似乎没有帮助(或者我没有正确理解它): Pylons FormEncode @validate 装饰器将参数传递到重新渲染操作
我有一个简单的表单,它接受所需的查询字符串(id)值,使用作为隐藏表单字段值,并验证发布的数据。控制器看起来像这样:
class NewNodeForm(formencode.Schema):
parent_id = formencode.validators.Int(not_empty = True)
child_name = formencode.validators.String(not_empty = True)
def newnode(self, id):
c.parent_id = id
return render('newnode.html')
@validate(schema=NewNodeForm(), form='newnode')
def createnode(self):
parentId = self.form_result.get('parent_id')
childName = self.form_result.get('child_name')
nodeId = save_the_data(parentId, childName)
return redirect_to(controller = 'node', action = 'view', id = nodeId)
表单非常基本:
<form method="post" action="/node/createnode">
<input type="text" name="child_name">
<input type="hidden" value="${c.parent_id}" name="parent_id">
<input name="submit" type="submit" value="Submit">
</form>
如果验证通过,一切都会正常工作,但如果失败,则无法调用 newnode
因为 id
不是' t 传回来了。它会抛出TypeError: newnode() gets正好2个参数(给定1个)
。简单地定义为 newnode(self, id = None) 可以解决这个问题,但我不能这样做,因为逻辑需要 id。
这看起来很简单,但我错过了什么?
My question may be the same as this, but the suggested answer didn't seem to help (or I didn't understand it correctly): Pylons FormEncode @validate decorator pass parameters into re-render action
I have a simple form that takes a required querystring (id) value, uses that as a hidden form field value, and validates the posted data. The controller looks like this:
class NewNodeForm(formencode.Schema):
parent_id = formencode.validators.Int(not_empty = True)
child_name = formencode.validators.String(not_empty = True)
def newnode(self, id):
c.parent_id = id
return render('newnode.html')
@validate(schema=NewNodeForm(), form='newnode')
def createnode(self):
parentId = self.form_result.get('parent_id')
childName = self.form_result.get('child_name')
nodeId = save_the_data(parentId, childName)
return redirect_to(controller = 'node', action = 'view', id = nodeId)
and the form is very basic:
<form method="post" action="/node/createnode">
<input type="text" name="child_name">
<input type="hidden" value="${c.parent_id}" name="parent_id">
<input name="submit" type="submit" value="Submit">
</form>
Everything works fine if the validation passes, but if it fails, newnode
can't be called because id
isn't passed back. It throws TypeError: newnode() takes exactly 2 arguments (1 given)
. Simply defining as newnode(self, id = None)
gets around this problem, but I can't do that as the id is required by the logic.
This seems so simple, but what am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在 newnode 中使用 id arg,我的偏好是在相关的 createnode 函数中使用相同的 arg。调整您的帖子网址以使用 id,并且您不需要隐藏parent_id,因为它现在是网址的一部分。
If you are using an id arg in your newnode my preference would be to use the same arg in it's related createnode function. Adapt your post url to use an id, and you wont need to hidden the parent_id because it is now part of the url.
当验证失败时,
validate
装饰器会使用修改后的request
对象调用您的newnode
,但所有 GET/POST 参数不得更改When validation failed the
validate
decorator call younewnode
with modifiedrequest
object, but all GET/POST arguments must be not changed