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

发布于 2024-10-13 05:55:05 字数 1343 浏览 3 评论 0原文

我的问题可能与此相同,但建议的答案似乎没有帮助(或者我没有正确理解它): 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

面如桃花 2024-10-20 05:55:05

如果您在 newnode 中使用 id arg,我的偏好是在相关的 createnode 函数中使用相同的 arg。调整您的帖子网址以使用 id,并且您不需要隐藏parent_id,因为它现在是网址的一部分。

<form method="post" action="/node/createnode/${request.urlvars['id']}">
  <input type="text" name="child_name">
  <input name="submit" type="submit" value="Submit">
</form>

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.

<form method="post" action="/node/createnode/${request.urlvars['id']}">
  <input type="text" name="child_name">
  <input name="submit" type="submit" value="Submit">
</form>
眼前雾蒙蒙 2024-10-20 05:55:05

当验证失败时,validate 装饰器会使用修改后的 request 对象调用您的 newnode,但所有 GET/POST 参数不得更改

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

When validation failed the validate decorator call you newnode with modified request object, but all GET/POST arguments must be not changed

def newnode(self, id=None):
  c.parent_id = id or request.params.get('parent_id')
  return render('newnode.html')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文