虽然建议使用以下构造来检查请求是否为POST,
if request.method == 'POST':
pass
人们很可能会找到
if request.POST:
pass
更优雅和简洁。
除个人偏好外,有没有理由不使用它?
答案 0 :(得分:13)
文档清楚地表明了这一点:
请求可以通过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 False