【发布时间】:2023-03-14 22:15:02
【问题描述】:
Since Django 1.5 raw post data is accessible via request.body.
在我的应用程序中,我有时会通过表单获取数据,有时还会获取原始数据(例如 json)。 有没有办法写出这样一个不会失败的函数?
def get_post_var(request, name):
result = request.POST.get(name)
if result:
return result
post_body = dict(urlparse.parse_qsl(request.body))
result = post_body.get(name)
if result:
return result
return None
【问题讨论】:
-
“有时是纯数据”是什么意思。如果是 POST 请求,django 将负责填充 request.POST,无论数据是通过表单提交还是通过 curl 或其他任何方式提交。
-
我的意思是非表单数据(例如 json),如下所述:docs.djangoproject.com/en/1.5/ref/request-response/…
标签: django exception post request