mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-02 20:54:42 +03:00
Tweak view slightly
This commit is contained in:
parent
6a15556384
commit
3e862c7737
|
@ -50,9 +50,9 @@ You can also set the authentication policy on a per-view basis, using the `APIVi
|
||||||
|
|
||||||
Or, if you're using the `@api_view` decorator with function based views.
|
Or, if you're using the `@api_view` decorator with function based views.
|
||||||
|
|
||||||
@api_view('GET'),
|
@api_view(('GET',)),
|
||||||
@authentication_classes(SessionAuthentication, UserBasicAuthentication)
|
@authentication_classes((SessionAuthentication, UserBasicAuthentication))
|
||||||
@permissions_classes(IsAuthenticated)
|
@permissions_classes((IsAuthenticated,))
|
||||||
def example_view(request, format=None):
|
def example_view(request, format=None):
|
||||||
content = {
|
content = {
|
||||||
'user': unicode(request.user), # `django.contrib.auth.User` instance.
|
'user': unicode(request.user), # `django.contrib.auth.User` instance.
|
||||||
|
|
|
@ -60,6 +60,8 @@ For example if your API serves JSON responses and the HTML browseable API, you m
|
||||||
|
|
||||||
If your API includes views that can serve both regular webpages and API responses depending on the request, then you might consider making `TemplateHTMLRenderer` your default renderer, in order to play nicely with older browsers that send [broken accept headers][browser-accept-headers].
|
If your API includes views that can serve both regular webpages and API responses depending on the request, then you might consider making `TemplateHTMLRenderer` your default renderer, in order to play nicely with older browsers that send [broken accept headers][browser-accept-headers].
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
# API Reference
|
# API Reference
|
||||||
|
|
||||||
## JSONRenderer
|
## JSONRenderer
|
||||||
|
@ -119,6 +121,8 @@ If you're building websites that use `HTMLTemplateRenderer` along with other ren
|
||||||
|
|
||||||
To implement a custom renderer, you should override `BaseRenderer`, set the `.media_type` and `.format` properties, and implement the `.render(self, data, media_type)` method.
|
To implement a custom renderer, you should override `BaseRenderer`, set the `.media_type` and `.format` properties, and implement the `.render(self, data, media_type)` method.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
# Advanced renderer usage
|
# Advanced renderer usage
|
||||||
|
|
||||||
You can do some pretty flexible things using REST framework's renderers. Some examples...
|
You can do some pretty flexible things using REST framework's renderers. Some examples...
|
||||||
|
@ -128,6 +132,8 @@ You can do some pretty flexible things using REST framework's renderers. Some e
|
||||||
* Specify multiple types of HTML representation for API clients to use.
|
* Specify multiple types of HTML representation for API clients to use.
|
||||||
* Underspecify a renderer's media type, such as using `media_type = 'image/*'`, and use the `Accept` header to vary the encoding of the response.
|
* Underspecify a renderer's media type, such as using `media_type = 'image/*'`, and use the `Accept` header to vary the encoding of the response.
|
||||||
|
|
||||||
|
## Varying behaviour by media type
|
||||||
|
|
||||||
In some cases you might want your view to use different serialization styles depending on the accepted media type. If you need to do this you can access `request.accepted_renderer` to determine the negotiated renderer that will be used for the response.
|
In some cases you might want your view to use different serialization styles depending on the accepted media type. If you need to do this you can access `request.accepted_renderer` to determine the negotiated renderer that will be used for the response.
|
||||||
|
|
||||||
For example:
|
For example:
|
||||||
|
|
|
@ -23,7 +23,7 @@ class BaseView(views.APIView):
|
||||||
"""
|
"""
|
||||||
return {
|
return {
|
||||||
'request': self.request,
|
'request': self.request,
|
||||||
'format': self.format,
|
'format': self.format_kwarg,
|
||||||
'view': self
|
'view': self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -162,13 +162,19 @@ class APIView(View):
|
||||||
"""
|
"""
|
||||||
return [throttle(self) for throttle in self.throttle_classes]
|
return [throttle(self) for throttle in self.throttle_classes]
|
||||||
|
|
||||||
|
def get_content_negotiator(self):
|
||||||
|
"""
|
||||||
|
Instantiate and return the content negotiation class to use.
|
||||||
|
"""
|
||||||
|
return self.content_negotiation_class()
|
||||||
|
|
||||||
def perform_content_negotiation(self, request, force=False):
|
def perform_content_negotiation(self, request, force=False):
|
||||||
"""
|
"""
|
||||||
Determine which renderer and media type to use render the response.
|
Determine which renderer and media type to use render the response.
|
||||||
"""
|
"""
|
||||||
renderers = self.get_renderers()
|
renderers = self.get_renderers()
|
||||||
conneg = self.content_negotiation_class()
|
conneg = self.get_content_negotiator()
|
||||||
return conneg.negotiate(request, renderers, self.format, force)
|
return conneg.negotiate(request, renderers, self.format_kwarg, force)
|
||||||
|
|
||||||
def has_permission(self, request, obj=None):
|
def has_permission(self, request, obj=None):
|
||||||
"""
|
"""
|
||||||
|
@ -198,7 +204,7 @@ class APIView(View):
|
||||||
"""
|
"""
|
||||||
Runs anything that needs to occur prior to calling the method handlers.
|
Runs anything that needs to occur prior to calling the method handlers.
|
||||||
"""
|
"""
|
||||||
self.format = self.get_format_suffix(**kwargs)
|
self.format_kwarg = self.get_format_suffix(**kwargs)
|
||||||
|
|
||||||
if not self.has_permission(request):
|
if not self.has_permission(request):
|
||||||
self.permission_denied(request)
|
self.permission_denied(request)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user