Python FormEncode验证失败后,使用querystring参数重新提交挂架表单
我的问题可能与此相同,但建议的答案似乎没有帮助(或者我没有正确理解): 我有一个简单的表单,它接受所需的querystring(id)值,将其用作隐藏的表单字段值,并验证发布的数据。控制器如下所示:Python FormEncode验证失败后,使用querystring参数重新提交挂架表单,python,pylons,validation,formencode,Python,Pylons,Validation,Formencode,我的问题可能与此相同,但建议的答案似乎没有帮助(或者我没有正确理解): 我有一个简单的表单,它接受所需的querystring(id)值,将其用作隐藏的表单字段值,并验证发布的数据。控制器如下所示: class NewNodeForm(formencode.Schema): parent_id = formencode.validators.Int(not_empty = True) child_name = formencode.validators.String(not_empty
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
没有传回。它抛出TypeError:newnode()正好接受2个参数(给定1个)
。简单地定义为newnode(self,id=None)
可以解决这个问题,但我不能这样做,因为逻辑需要id
这看起来很简单,但是我缺少什么呢?当验证失败时,
验证
装饰程序使用修改的请求
对象调用您的新节点
,但所有GET/POST参数都不能更改
def newnode(self, id=None):
c.parent_id = id or request.params.get('parent_id')
return render('newnode.html')
如果您在newnode中使用id参数,我的首选是在其相关的createnode函数中使用相同的参数。调整您的帖子url以使用id,您就不需要隐藏父id,因为它现在是url的一部分
<form method="post" action="/node/createnode/${request.urlvars['id']}">
<input type="text" name="child_name">
<input name="submit" type="submit" value="Submit">
</form>