gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:29:24 24 4
gpt4 key购买 nike

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

我有一个简单的表单,它采用必需的查询字符串 (id) 值,将其用作隐藏的表单字段值,并验证发布的数据。 Controller 看起来像这样:

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。调整您的帖子 url 以使用 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>

关于python - 在 FormEncode 验证失败后使用查询字符串参数重新呈现 Pylons 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4742842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com