我正在开发一个 Google App Engine 项目,该项目收集用户提交的故事。
这是我在请求处理程序的 post 方法中处理提交错误的方法:
# get the title and content using self.request.get()
errors = []
if not title:
errors.append("Please enter a title.")
if not content:
errors.append("Please enter a story.")
if not errors:
# create the story, save it to the database
# redirect to the story's page
else:
# pass the title and/or content to a template
# pass the error message(s) to a template
# the same template that displays the submission form is used here
问题:因为我的表单将帖子发送到example.com/createstory.do - 如果出现错误,我最终会在该地址重新显示表单页面。
我想要发生的事情:将用户重定向回提交表单的页面:example.com/Share,同时显示错误消息并重新显示提交的表单数据。
最简单的方法是什么?
我知道我可以让 /Share 处理 get 和 post 请求,但我正在寻找一个可以使用的解决方案,即使这样做不是一个选择。
最佳答案
如果您绝对确定需要使用单独的 URL,则可以重定向到/Share,包括 URL 中 GET 变量中的错误。当然,这会使您的 URL 变得丑陋,因为它现在包含所有错误信息。
另一种选择是重定向回共享并将错误存储在 cookie 或 session 变量中。
在提交之前将其中任何一个与 Javascript 中的客户端表单验证相结合,这样在大多数情况下就不会遇到丑陋的解决方案,这可能是最好的选择。
关于python - 如何使用 Google App Engine 重定向并显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1538287/