FormEncode验证失败后,使用查询字符串参数重新呈现Pylons表单

我的问题可能与此相同,但建议的答案似乎没有帮助(或者我没有正确理解):Pylons FormEncode @validate decorator将参数传递给重新渲染动作 我有一个简单的表单,它采用必需的查询字符串(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
不会被传回。它抛出
TypeError: newnode() takes exactly 2 arguments (1 given)
。简单地定义为
newnode(self, id = None)
可以解决这个问题,但我不能这样做,因为逻辑需要id。 这看起来很简单,但我错过了什么?     
已邀请:
如果你在newnode中使用id arg,我的偏好是在它的相关createnode函数中使用相同的arg。调整你的帖子网址以使用id,你不需要隐藏parent_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>
    
验证失败后,
validate
装饰器会使用修改后的
request
对象调用
newnode
,但所有GET / POST参数都不能更改
def newnode(self, id=None):
  c.parent_id = id or request.params.get('parent_id')
  return render('newnode.html')
    

要回复问题请先登录注册