128👍
If you’re using a function based view, then this issue usually means you forgot to add the @api_view and the @renderer_classes decorator to your view.
Example:
from rest_framework.decorators import api_view, renderer_classes
from rest_framework.renderers import JSONRenderer, TemplateHTMLRenderer
@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def get_assessment_count(request):
[...]
data = {'count': queryset.count()}
return Response(data, template_name='assessments.html')
19👍
If anyone has this issue with Django Rest Framework while using Response in a function based view, don’t forget to use @APIView instead of View decorator.
- [Django]-Django CMS fails to synch db or migrate
- [Django]-How to mock users and requests in django
- [Django]-How can I get tox and poetry to work together to support testing multiple versions of a Python dependency?
3👍
In addition to the accepted answer by @DavidLam it could also be that an error was thrown in your view/handler4xx/handler5xx and you’ve not caught it appropriately.
- [Django]-How do I reference a Django settings variable in my models.py?
- [Django]-CORS error while consuming calling REST API with React
- [Django]-Auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'UserManage.groups'
0👍
When developing web API you must use API decorator to handle request and generate responses, so you should use @api_view(['GET','POST'])
before functions.
- [Django]-Change a field in a Django REST Framework ModelSerializer based on the request type?
- [Django]-Python Socket.IO client for sending broadcast messages to TornadIO2 server
- [Django]-Can a dictionary be passed to django models on create?
0👍
This could be due to overriding the dispatch method of a view or ViewSet. If you override that method and return without running the base/super version of the function, the renderer wont be setup on the response.
This is the line that sets the renderer on the response, in the source for APIView:
self.response = self.finalize_response(request, response, *args, **kwargs)
- [Django]-Django using get_user_model vs settings.AUTH_USER_MODEL
- [Django]-AssertionError: database connection isn't set to UTC
- [Django]-WARNING: Running pip as the 'root' user