Title: Rendering templates with Django REST Framework
Tags: python;django;django-rest-framework
Question: Right now I'm helping a friend out with his site that's built with Django REST Framework. I'm not really familiar with it so when I opened the module that contained the views I was confused as to where I need to load the template for the view:
class ProfileView(APIView):
permission_classes = [IsAuthenticated]
def get(self, request):
serialized = UserProfileSer(instance=request.user)
return Response(serialized.data)
def post(self, request):
serialized = UserProfileSer(instance=request.user, data=request.data, partial=True)
if serialized.is_valid():
serialized.save()
return Response(serialized.data)
return Response(serialized._errors, status=status.HTTP_400_BAD_REQUEST)
I'm used to doing return render(request, 'some_template.html', context)
I know what serializing does basically but I don't know how to use it to load a template or if I'm supposed to. Sorry
Comment: I didn't use it, that's what I'm used to using when I'm doing my own project. I'm wondering how to render the template with the code that is given, or if it's even possible.

Comment: when you  use this  "return render(request, 'some_template.html', context)" did got any error


Here is another answer: as per the rest framework document, try this
class UserDetail(generics.RetrieveAPIView):
"""
A view that returns a templated HTML representation of a given user.
"""
queryset = User.objects.all()
renderer_classes = (TemplateHTMLRenderer,)
def get(self, request, *args, **kwargs):
self.object = self.get_object()
return Response({'user': self.object}, template_name='user_detail.html')
If you want know more the refer the API docs

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:11/03/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><django><django-rest-framework>', 'Title': 'Rendering templates with Django REST Framework', 'ViewCount': 3853, 'id': 47088201, 'user_AboutMe': '<p>Pursuing an educated way of life, in every sense of the word.</p>\n', 'user_AccountId': 2501052.0, 'user_DisplayName': 'Amon', 'user_Reputation': 2503.0, 'user_Views': 444.0, 'user_id': 2621316.0}}