mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-27 08:29:59 +03:00
Update docs about TemplateHTMLRenderer
Provide an example demonstrating this renderer can now be interoperable with JSONRenderer.
This commit is contained in:
parent
4c877d2170
commit
84474e7dd7
|
@ -100,18 +100,9 @@ The default JSON encoding style can be altered using the `UNICODE_JSON` and `COM
|
||||||
|
|
||||||
Renders data to HTML, using Django's standard template rendering.
|
Renders data to HTML, using Django's standard template rendering.
|
||||||
Unlike other renderers, the data passed to the `Response` does not need to be serialized. Also, unlike other renderers, you may want to include a `template_name` argument when creating the `Response`.
|
Unlike other renderers, the data passed to the `Response` does not need to be serialized. Also, unlike other renderers, you may want to include a `template_name` argument when creating the `Response`.
|
||||||
|
The `TemplateHTMLRenderer` will create the template context with keys `data`, `request`, `response`, `view` and determine a template name to use to render it.
|
||||||
|
|
||||||
The TemplateHTMLRenderer will create a `RequestContext`, using the `response.data` as the context dict, and determine a template name to use to render the context.
|
**Note:** For compatibility reasons, the context may contain all response data at the top level as well as the `status_code` key.
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
**Note:** When used with a view that makes use of a serializer the `Response` sent for rendering may not be a dictionary and will need to be wrapped in a dict before returning to allow the TemplateHTMLRenderer to render it. For example:
|
|
||||||
|
|
||||||
```
|
|
||||||
response.data = {'results': response.data}
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
The template name is determined by (in order of preference):
|
The template name is determined by (in order of preference):
|
||||||
|
|
||||||
|
|
|
@ -17,25 +17,24 @@ Here's an example of a view that returns a list of "Profile" instances, rendered
|
||||||
**views.py**:
|
**views.py**:
|
||||||
|
|
||||||
from my_project.example.models import Profile
|
from my_project.example.models import Profile
|
||||||
from rest_framework.renderers import TemplateHTMLRenderer
|
from my_project.example.serializers import ProfileSerializer
|
||||||
|
from rest_framework.generics import ListAPIView
|
||||||
|
from rest_framework.renderers import JSONPRenderer, TemplateHTMLRenderer
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.views import APIView
|
|
||||||
|
|
||||||
|
|
||||||
class ProfileList(APIView):
|
class ProfileListView(ListAPIView):
|
||||||
renderer_classes = [TemplateHTMLRenderer]
|
queryset = Profile.objects.all()
|
||||||
|
serializer_class = ProfileSerializer
|
||||||
|
renderer_classes = [JSONPRenderer, TemplateHTMLRenderer]
|
||||||
template_name = 'profile_list.html'
|
template_name = 'profile_list.html'
|
||||||
|
|
||||||
def get(self, request):
|
|
||||||
queryset = Profile.objects.all()
|
|
||||||
return Response({'profiles': queryset})
|
|
||||||
|
|
||||||
**profile_list.html**:
|
**profile_list.html**:
|
||||||
|
|
||||||
<html><body>
|
<html><body>
|
||||||
<h1>Profiles</h1>
|
<h1>Profiles</h1>
|
||||||
<ul>
|
<ul>
|
||||||
{% for profile in profiles %}
|
{% for profile in data %}
|
||||||
<li>{{ profile.name }}</li>
|
<li>{{ profile.name }}</li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
|
@ -55,7 +54,7 @@ The following view demonstrates an example of using a serializer in a template f
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
|
|
||||||
|
|
||||||
class ProfileDetail(APIView):
|
class ProfileDetailView(APIView):
|
||||||
renderer_classes = [TemplateHTMLRenderer]
|
renderer_classes = [TemplateHTMLRenderer]
|
||||||
template_name = 'profile_detail.html'
|
template_name = 'profile_detail.html'
|
||||||
|
|
||||||
|
@ -66,7 +65,7 @@ The following view demonstrates an example of using a serializer in a template f
|
||||||
|
|
||||||
def post(self, request, pk):
|
def post(self, request, pk):
|
||||||
profile = get_object_or_404(Profile, pk=pk)
|
profile = get_object_or_404(Profile, pk=pk)
|
||||||
serializer = ProfileSerializer(profile, data=request.data)
|
serializer = ProfileSerializer(profile, request.data)
|
||||||
if not serializer.is_valid():
|
if not serializer.is_valid():
|
||||||
return Response({'serializer': serializer, 'profile': profile})
|
return Response({'serializer': serializer, 'profile': profile})
|
||||||
serializer.save()
|
serializer.save()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user