python - 如何使用 Google App Engine 重定向并显示错误

标签 python google-app-engine

我正在开发一个 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/

相关文章:

google-app-engine - 如何使用 Java 指定遗留增量 ID

python - 正则表达式匹配一些子字符串,同时忽略看似相同的子字符串?

google-app-engine - 如何在 GWT 中设置 CSS 样式颜色

python - 理解这个 python 函数的复杂性

python - 如何从 python 脚本更改 python 文件中的变量值

google-app-engine - DeadlineExceededError : The API call taskqueue. BulkAdd()响应时间过长被取消

angular - 使用 Azure AD 保护单页 Google 应用引擎 Web 应用

java - 尽管所需的索引是 'serving' ,但为什么我的 AppEngine 查询会抛出 DatastoreNeedIndexExceptions ?

python - 个人资料图片或头像的电子邮件标题?

python - lxml,Python。我无法获取元素 : element. 文本的内容为 None