在 Pyramid 中 POST 后如何重定向?
我试图将我的表单提交到一个路由,该路由将验证数据然后重定向回原始路由。
例如:
- 用户加载页面 website.com/post
- 表单将数据发布到 website.com/post-save
- 用户被重定向回 website.com/post
金字塔在执行此操作时给我带来了一些麻烦。
这是我精简后的views.py
def _get_link_form(post_data):
""" Returns the initialised form object """
return LinkForm(post_data)
def home_page(request):
form = _get_link_form(request.POST)
return {'form' : form}
def save_post(request):
""" form data is submitted here """"
form = _get_link_form(request.POST)
if not form.validate():
return home_page(request, form)
这是我一直在使用的代码。它不仅不起作用,而且还给人一种凌乱、被砍掉的感觉。当然有一种更简单的方法可以在 Pyramid 中“POST 后重定向”吗?
I'm trying to have my form submit to a route which will validate the data then redirect back to the original route.
For example:
- User loads the page website.com/post
- Form POSTs the data to website.com/post-save
- User gets redirected back to website.com/post
Pyramid is giving me some troubles doing this.
Here's my slimmed down views.py
def _get_link_form(post_data):
""" Returns the initialised form object """
return LinkForm(post_data)
def home_page(request):
form = _get_link_form(request.POST)
return {'form' : form}
def save_post(request):
""" form data is submitted here """"
form = _get_link_form(request.POST)
if not form.validate():
return home_page(request, form)
This is the code I've been playing around with. Not only does it not work, it also feels messy and hacked up. Surely there's a simpler way to 'redirect after-POST' in Pyramid?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的问题最容易解决,只需 POST 到表单显示的同一 URL,并在 POST 成功时将用户重定向到页面之外。这样,在表单成功提交之前,您无需更改 URL。
如果您只是想 POST 到不同的 URL,那么您需要使用会话保存数据,因为您显然是在请求之间处理表单数据。
通常,如果您希望能够处理表单中的错误,您将使用会话和闪现消息。为此,您只需添加一个位置,以便在基本模板中显示 Flash 消息,并使用诸如 Pyramid_beaker 之类的工具设置会话支持。
假设您的主页设置在“home”命名路由上:
请注意,如果表单无法验证您使用与最初呈现表单相同的代码,并且只有在成功时才进行重定向。此格式还可以处理使用提交中使用的值和默认值填充表单。
您可以使用
request.session.peek_flash()
和request.session.pop_flash()
循环浏览所选模板中的 Flash 消息。如果您想标记主页视图以检查会话数据,
route_url
还支持更改生成的 url 上的查询字符串。显然,您可以将查询字符串中的所有内容传递回主页,但这是一个相当大的安全漏洞,会话可以帮助防范。
Your problem is most easily solved by simply POSTing to the same URL that your form is shown at, and simply redirecting the user away from the page when the POST is successful. That way until the form is successfully submitted you do not change URLs.
If you're just dying to POST to a different URL, then you need to save the data using sessions, since you're obviously handling the form data between requests.
Typically if you want to be able to handle errors in your forms you would use a session and flash messages. To do this you simply add a location for flash messages to appear in your base template and setup session support using something like pyramid_beaker.
Assuming your home page is setup at the 'home' named-route:
Notice how if the form fails to validate you use the same code you did to render the form originally, and only if it is successful do you redirect. This format can also handle populating the form with the values used in the submission, and default values.
You can loop through the flash messages in your template of choice using
request.session.peek_flash()
andrequest.session.pop_flash()
.route_url
supports mutating the query string on the generated url as well, if you want to flag your home page view to check the session data.You can obviously just pass everything in the query string back to the home page, but that's a pretty big security vulnerability that sessions can help protect against.
金字塔文档有一个特别重要的部分,示例如下:
The Pyramid documentation has a particularly on-point section with the following example:
我这样做:
这会发送 HTTP 创建的代码,201
I do this like so:
This sends the HTTP Created code , 201
Pyramid 文档包含有关重定向的内容,您可以在以下链接中查看更多信息:
金字塔文档
编辑:
实际上,您可以使用 Javascript 在客户端执行此操作,首先您应该向客户端发送特定响应(闪烁一些数据或返回 Response 对象):
The Pyramid documentation has content about Redirect, you can see more information in below link :
Pyramid documentation
Edited:
Actually you can do that on client side with Javascript, first you should send particular response to client side(either with flashes some data or return Response object):
假设您的主页是金字塔网络应用程序的默认视图,您可以执行以下操作:
基本上您需要知道 home_page 视图是如何“添加”到您的配置器中的。如果您的主页实际上位于 /few/levels/deep/homepage ,那么重定向可能如下所示:
Assuming your homepage is the default view of your pyramid web app, you can do:
Basically you need to know how the home_page view was "added" to your Configurator. If your homepage actually lives at /few/levels/deep/homepage then a redirect might look like this:
一种干净的方法是使用金字塔为不同的请求类型提供的“重载”,例如,您可以这样装饰您的方法:
在 HTML 中,您必须调用操作表单,例如
这样在使用POST方法的时候处理一个方法,另一个当使用 GET 时。相同的名称,但有两种实现。
A clean way is using the "overload" provided by pyramid for different request types, por example, you can decorate your methods this way:
In the HTML, you must call the action form, like
<form method="POST" id="tform" action="${request.route_url('home', action='save')}">
This way, one method is processed when the method POST is used, and the other when the GET is used. The same name, but two implementations.