Requests

If you're doing REST-based web service stuff ... you should ignore request.POST.
如果你正在使用基于REST的web服务,你应该忽视掉request.POST.
— Malcom Tredinnick, Django developers group

REST framework's Request class extends the standard HttpRequest, adding support for REST framework's flexible request parsing and request authentication.

REST framework的Request类拓展了标准的HttpRequest,添加了对REST framework灵活的请求解析和请求认证。

Request parsing
REST framework's Request objects provide flexible request parsing that allows you to treat requests with JSON data or other media types in the same way that you would normally deal with form data.
REST framework请求对象提供灵活的请求解析,使你可以使用JSON数据或者其他媒介类型来处理请求,就和你通常处理数据表单一样。

.data
request.data returns the parsed content of the request body. This is similar to the standard request.POST and request.FILES attributes except that:
request.data 返回被解析的请求内容。很像标准的request.POST和request.FILES属性,除了:

  • It includes all parsed content, including file and non-file inputs.
  • 它包含所有被解析的内容,包括文件和非文件的输入。
  • It supports parsing the content of HTTP methods other than POST,
    meaning that you can access the content of PUT and PATCH requests.
  • 它不止支持解析POST方法提交的内容,意味着你可以通过PUT和PATCH方法发出请求。
  • It supports REST framework's flexible request parsing, rather than
    just supporting form data. For example you can handle incoming JSON
    data in the same way that you handle incoming form data. For more
    details see the parsers documentation.
  • 它支持REST framework灵活的请求解析,而不只是支持表单数据。例如你可以用和处理表单数据一样的方式来处理即将到来的JSON数据。想要了解更多详细信息请查看解析文档。

.query_params
request.query_params is a more correctly named synonym for request.GET.
request.query_params和request.GET几乎是一个意思。

For clarity inside your code, we recommend using request.query_params instead of the Django's standard request.GET. Doing so will help keep your codebase more correct and obvious - any HTTP method type may include query parameters, not just GET requests.
为了你的code清晰,我们建议使用request.query_params来取代Django的标准request.GET.以此来保持你的codebase更加正确和严谨。-任何HTTP方法类型都可能包含在踩查询参数中,不只是GET请求。

.parsers
The APIView class or @api_view decorator will ensure that this property is automatically set to a list of Parser instances, based on the parser_classes set on the view or based on the DEFAULT_PARSER_CLASSES setting.
APIView类和@api_view装饰器将确保这个属性被自动设置为一组parser实例。根据设置在view上的parser_classes或者DEFAULT_PARSER_CLASSES中的设置。

You won't typically need to access this property.
你通常不需要考虑这个属性。

Note: If a client sends malformed content, then accessing request.data may raise a ParseError. By default REST framework's APIView class or @api_view decorator will catch the error and return a 400 Bad Request response.
注意:如果客户端发送一个畸形的内容,访问request.data可能会抛出一个ParseError。默认的REST framework的APIView类和@api_view装饰器将会捕获这个错误并且返回400失败请求响应。

If a client sends a request with a content-type that cannot be parsed then a UnsupportedMediaType exception will be raised, which by default will be caught and return a 415 Unsupported Media Type response.
如果客户端发送一个内容无法被解析的请求,一个UnsupportedMediaType异常将会被抛出,默认的它将会捕获然后返回一个415不支持的媒介类型响应。

Content negotiation
The request exposes some properties that allow you to determine the result of the content negotiation stage. This allows you to implement behaviour such as selecting a different serialisation schemes for different media types.
request公开一些属性用于允许你来决定内容协议阶段的结果。这允许你执行一些行为,比如为不同的媒介类型选择不同的序列化模式。

.accepted_renderer
The renderer instance what was selected by the content negotiation stage.
渲染器实例由内容协商阶段决定。

.accepted_media_type
.A string representing the media type that was accepted by the content negotiation stage.
一个表示被内容协商阶段所接受的媒介类型的数组。

Authentication
REST framework provides flexible, per-request authentication, that gives you the ability to:
REST framework提供灵活的,每一个请求的认证,让你可以实现:

Use different authentication policies for different parts of your API.
使用不同的认证规则到不同的API部分。
Support the use of multiple authentication policies.
支持使用多种认证规则。
Provide both user and token information associated with the incoming request.
同时提供与传入请求相关的user和token信息。

.user
request.user typically returns an instance of django.contrib.auth.models.User, although the behavior depends on the authentication policy being used.
request.user 通常返回一个jango.contrib.auth.models.User的实例,尽管这个行为取决于被使用的用户认证规则。

If the request is unauthenticated the default value of request.user is an instance of django.contrib.auth.models.AnonymousUser.
如果这个请求未被认证则默认返回的request.user是django.contrib.auth.models.AnonymousUser的一个实例

For more details see the authentication documentation.
了解更多详情参见认证文档。

.auth
request.auth returns any additional authentication context. The exact behavior of request.auth depends on the authentication policy being used, but it may typically be an instance of the token that the request was authenticated against.
request.auth 返回任何额外的认证上下文。准确的request.auth行为取决于被使用的认证规则,但是它通常是一个请求已经被认证的token实例。

If the request is unauthenticated, or if no additional context is present, the default value of request.auth is None.
如果请求未被认证,或者如果没有附加的上下文,默认的request.auth是None。

For more details see the authentication documentation.
了解更多详情参见认证文档

.authenticators
The APIView class or @api_view decorator will ensure that this property is automatically set to a list of Authentication instances, based on the authentication_classes set on the view or based on the DEFAULT_AUTHENTICATORS setting.
APIView类或者@api_view装饰器将确保这个属性被自动设置为一组Authentication实例,根据视图上设置的authentication_classes或DEFAULT_AUTHENTICATORS上的设置

You won't typically need to access this property.
你通常不需要访问这个属性。

Browser enhancements
REST framework supports a few browser enhancements such as browser-based PUT, PATCH and DELETE forms.
REST framework支持一部分浏览器增强,比如基于浏览器的PUT,PATCH和DELETE表单。

.method
request.method returns the uppercased string representation of the request's HTTP method.
request.method返回HTTP请求方法的大写字符串。

Browser-based PUT, PATCH and DELETE forms are transparently supported.
基于浏览器的PUT,PATCH和DELETE表单显然也是被支持的。

For more information see the browser enhancements documentation.
了解更多详情参见浏览器增强文档。

.content_type
request.content_type, returns a string object representing the media type of the HTTP request's body, or an empty string if no media type was provided.
request.content_type返回一个表示HTTP请求主体的媒介类型的字符串对象,否则如果没有媒介类型被提供的话就提供一个空的字符串。

You won't typically need to directly access the request's content type, as you'll normally rely on REST framework's default request parsing behavior.
你通常不需要直接访问请求的内容类型,就像你通常依赖于REST framework默认的请求解析行为。

If you do need to access the content type of the request you should use the .content_type property in preference to using request.META.get('HTTP_CONTENT_TYPE'), as it provides transparent support for browser-based non-form content.
如果你确实需要访问请求的内容类型,你应该使用.content_type属性request.META.get('HTTP_CONTENT_TYPE'),因为它提供了对基于浏览器non-form内容的明确支持。

For more information see the browser enhancements documentation.
了解更多详情参见浏览器增强文档。

.stream
request.stream returns a stream representing the content of the request body.
request.stream返回一个代表请求内容主体的流。

You won't typically need to directly access the request's content, as you'll normally rely on REST framework's default request parsing behavior.
你通常不需要直接访问请求的内容,就像你通常依赖于REST framework默认的请求解析行为一样。

Standard HttpRequest attributes
As REST framework's Request extends Django's HttpRequest, all the other standard attributes and methods are also available. For example the request.META and request.session dictionaries are available as normal.
因为REST framework的Request继承了Django的HttpRequest,所有其他的标准属性和方法一样可以使用。例如request.META和request.session字典和平常一样可用。

Note that due to implementation reasons the Request class does not inherit from HttpRequest class, but instead extends the class using composition.
注意由于实现原因,Request类不是继承于HttpRequest类,取而代之的是使用组合扩展类。


走神
1 声望0 粉丝

学艺不精 无能狂怒


引用和评论

0 条评论