AssertionError when trying to render API data using djangorestframwork?


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:

@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def course_api(request):
    if request.method == 'GET':
        queryset = Course.objects.all()
        serializer = CourseNameSerializers(queryset, many=True)
        return Response(, 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


I was trying to find what was my problem and i found that I used

this is my for REST_FRAMEWORK

    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.

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