python - Pylons - 处理 GET 和 POST 请求

标签 python validation pylons

在我的 Pylons 应用程序中处理表单 POST 数据的最佳方式是什么?我试过:

  • 有一个单独的 GET 方法和一个带有 rest.restrict('post') 装饰器的 POST 方法。问题 - 如果存在验证错误,则无法使用用户输入的数据重新显示表单,因为您必须重定向回 GET 方法,或者必须直接从 POST 方法呈现模板。不幸的是,这看起来很奇怪,因为 URL 必须更改以对应于 POST 操作。

  • 将所有内容都集中在一个方法中,并通过检查 request.method 来检测表单是否已发布。这工作正常,但 if request.method == 'post': ... else: ... 似乎很笨拙

最佳答案

Having it all in one method, and detecting if the form has been posted via a check on request.method. This works okay, but it seems clumsy to have if request.method == 'post': ... else: ...

我不知道你为什么形容这是笨拙的。在跨语言的 Web 应用程序世界中,切换请求方法是一个有效的习惯用法。例如您会发现 Django View 有一个 View ,该 View 根据 request.method 以不同的方式处理请求。类似地,在 Java 中,Servlet 具有 doPost()doGet() 方法,为 GETPOST 提供不同的行为要求。

更新

I'd just rather have them separated into different methods, if possible. Many other web frameworks do this

这种方法也没有什么问题。我只是指出使用相同的方法处理它们同样有效。

关于python - Pylons - 处理 GET 和 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3871145/

相关文章:

java - 仅当验证为 True 时才启用 Jbutton

Pylons + mod_wsgi -> ImportError : No module named paste. 部署

python - 动态 SelectField 验证失败并显示 : "Not a valid choice"

python - 不重复python的排列

python - 在python中加载64位原始图像文件

javascript - 如何防止浏览器显示默认的 HTML 验证错误消息

python - 为什么图像卷积算子的方向不直观?

validation - Angular 2 : how to apply two or more validators to the same control?

python - 在 Python Pylons 中检测 ajax 与常规浏览器帖子

python - 类似于 Pylons for Ruby 的框架