虽然建议使用以下构造来检查请求是否为POST。
if request.method == 'POST':
pass人们很可能会发现
if request.POST:
pass更优雅、更简洁。
除了个人偏好之外,还有什么理由不使用它吗?
发布于 2010-05-30 12:24:08
文档中对此有明确的说明:
可以使用空的POST字典通过POST进入请求--例如,如果通过POST HTTP方法请求表单,但不包括表单数据。因此,您不应该使用if request.POST来检查POST方法的使用;相反,应该使用if request.method == "POST“(参见上文)。
>>> # assume an empty POST request would be treated as a dict
>>> bool({})
False
>>> # it would be a POST request, but request.POST would evaluate to Falsehttps://stackoverflow.com/questions/2937518
复制相似问题