AssertionError when trying to render API data using djangorestframwork?

Issue

I am trying to build a function API using djangorestframwork
But I have this error:

Cannot apply DjangoModelPermissionsOrAnonReadOnly on a view that does not set `.queryset` or have a `.get_queryset()` method.

This is my API function:

@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def course_api(request):
    if request.method == 'GET':
        queryset = Course.objects.all()
        serializer = CourseNameSerializers(queryset, many=True)
        return Response(serializer.data, template_name='api.html')

When I delete first line and second line another error occurs
and this error is:

.accepted_renderer not set on Response

Solution

I was trying to find what was my problem and i found that I used
REST_FRAMEWORK in settings.py file

this is my settings.py for REST_FRAMEWORK

REST_FRAMEWORK = {
    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
    ]
}

I removed these lines of code and the API works fine.

Answered By – DIAA SHALABY

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published