在FormEncode验证失败后使用querystring参数重新呈现Pylons表单

2024-04-29 15:20:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我的问题可能与此相同,但建议的答案似乎没有帮助(或者我没有正确理解):Pylons FormEncode @validate decorator pass parameters into re-render action

我有一个简单的表单,它接受所需的querystring(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)

形式非常基本:

^{pr2}$

如果验证通过,则一切正常,但如果验证失败,则无法调用newnode,因为id没有返回。它抛出TypeError: newnode() takes exactly 2 arguments (1 given)。简单地定义为newnode(self, id = None)就可以解决这个问题,但是我不能这样做,因为逻辑需要id。在

这看起来很简单,但我错过了什么?在


Tags: selfformidchildtruenotactionrender
2条回答

验证失败时,validate修饰符使用修改的request对象调用您newnode,但所有GET/POST参数都不能更改

def newnode(self, id=None):
  c.parent_id = id or request.params.get('parent_id')
  return render('newnode.html')

如果您在newnode中使用id arg,那么我的首选是在它的相关createnode函数中使用相同的参数。调整你的post url以使用一个id,你不需要隐藏父级的\u 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>

相关问题 更多 >