在我的 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()
方法,为 GET
和 POST
提供不同的行为要求。
更新
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/