How to pass django rest framework response to html

0 votes

How to pass django restframework response for any request to html. Example: A list which contains objects, and html be articles.html.

I tried by using rest framework Response :

data= {'articles': Article.objects.all() }
return Response(data, template_name='articles.html')

I am getting this error :

""" AssertionError at /articles/

.accepted_renderer not set on Response """

How to fix this error?

Jun 25, 2020 in Python by kartik
• 37,530 points
2,005 views

1 answer to this question.

0 votes

Hello @kartik,

You need to use an @api_view decorator to display properly. I've seen this particular error happen for this exact reason (missing API View declaration in function based views).

from rest_framework.decorators import api_view
# ....

@api_view(['GET', 'POST', ])
def articles(request, format=None):
    data= {'articles': Article.objects.all() }
    return Response(data, template_name='articles.html')

Hope this is helpful!

answered Jun 25, 2020 by Niroj
• 82,820 points

Related Questions In Python

0 votes
1 answer

how to download and install Django rest framework?

To install Django, you can simply open ...READ MORE

answered Apr 24, 2018 in Python by Christine
• 15,830 points
810 views
0 votes
1 answer

How to PATCH a single field using Django Rest Framework?

Hello @kartik, Serializers allow partial updates by specifying partial=True when initializing ...READ MORE

answered Jun 25, 2020 in Python by Niroj
• 82,820 points
6,462 views
0 votes
1 answer

How to add annotate data in django-rest-framework queryset responses?

Hello @kartik, The queryset returned from get_queryset provides ...READ MORE

answered Jun 25, 2020 in Python by Niroj
• 82,820 points
2,923 views
0 votes
1 answer

How to secure APIs for Registration and Login in Django Rest Framework?

Hello @kartik, you cannot have an authentication system ...READ MORE

answered Jun 25, 2020 in Python by Niroj
• 82,820 points
1,918 views
0 votes
1 answer

How to return the current user with Django Rest Framework?

Hello @kartik, The best way is to use ...READ MORE

answered Jun 25, 2020 in Python by Niroj
• 82,820 points
6,419 views
0 votes
1 answer

How to return custom JSON in Django REST Framework?

Hello @kartik, There are 2 ways to custom ...READ MORE

answered Jun 25, 2020 in Python by Niroj
• 82,820 points
7,000 views