Django Rest框架:request.Post与request.data? [英] Django Rest frameworks: request.Post vs request.data?

查看:725
本文介绍了Django Rest框架:request.Post与request.data?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django Rest Frameworks所说的是关于 POST 的话,引用了Django开发者

The Django Rest Frameworks has this to say about POST, quoting a Django dev


请求


如果您正在做基于REST的Web服务……您应该忽略request.POST。

Requests

If you're doing REST-based web service stuff ... you should ignore request.POST.

-Django开发人员小组Malcom Tredinnick

— Malcom Tredinnick, Django developers group

由于不是那么有经验的Web开发人员,为什么不推荐 request.POST (标准)超过 request.DATA (非标准)?

As not-so experienced web-developer, why is request.POST (standard) discouraged over request.DATA (non-standard)? Is one more flexible?

推荐答案

文档涵盖了以下内容:


request.data返回请求正文的解析内容。这与标准的request.POST和request.FILES属性类似,不同之处在于:

request.data returns the parsed content of the request body. This is similar to the standard request.POST and request.FILES attributes except that:


  • 它包括所有已解析的内容,包括文件和非文件输入。

  • 它支持解析POST以外的HTTP方法的内容,这意味着您可以访问PUT和PATCH请求的内容。

  • 它支持REST框架的灵活请求解析,而不仅仅是支持表单数据。例如,您可以以与处理传入表单数据相同的方式来处理传入JSON数据。

最后两个是重要的。通过始终使用 request.data 而不是 request.POST ,您同时支持JSON和表单编码的输入(或您配置的任何一组解析器),您将在 PUT PATCH 请求上接受请求内容,以及 POST

The last two are the important ones. By using request.data throughout instead of request.POST, you're supporting both JSON and Form-encoded inputs (or whatever set of parsers you have configured), and you'll be accepting request content on PUT and PATCH requests, as well as for POST.


是否更灵活?

Is one more flexible?

是。 request.data 更灵活。

这篇关于Django Rest框架:request.Post与request.data?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆