0

Why i am getting this error? Expected a Response, HttpResponse or HttpStreamingResponse to be returned from the view, but received a <class 'NoneType'> how do i solve this ?

my Homepage/api/views.py

from rest_framework import status
from rest_framework.response import Response
from rest_framework.decorators import api_view

from Homepage.models import EducationLevel
from Homepage.api.serializers import EducationLevelSerializer

@api_view(['GET', ])
def api_detail_educationlevel(request):

  try:
    education = EducationLevel.objects.all()
  except EducationLevel.DoesNotExist:
    return Response(status=status.HTTP_400_BAD_REQUEST)

    if request.method == "GET":
      serializer = EducationLevelSerializer(education)
      return Response(serializer.data)

Homepage/api/serializers.py

from rest_framework import serializers
from Homepage.models import EducationLevel
class EducationLevelSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = EducationLevel
        field = ('Sequence', 'Description', 'Status')

my Homepage/api/urls.py

from django.urls import path
from Homepage.api.views import api_detail_educationlevel

app_name='educationlevel'

urlpatterns = [
  path('', api_detail_educationlevel, name="detail"),
]

my main urls.py

urlpatterns = [

    path('api/educationlevel/', include('Homepage.api.urls', 'educationlevel_api')),
]

UPDATE

when i tried this

def api_detail_educationlevel(request, slug):
      try:
        education = EducationLevel.objects.get(id=slug)
      except EducationLevel.DoesNotExist:
        return Response(status=status.HTTP_400_BAD_REQUEST)

i get this error

enter image description here

  • Does this answer your question? [The view didn't return an HttpResponse object. It returned None instead](https://stackoverflow.com/questions/26258905/the-view-didnt-return-an-httpresponse-object-it-returned-none-instead) – Compro Prasad Jun 15 '20 at 14:33
  • @Compro Prasad no –  Jun 15 '20 at 14:35

4 Answers4

1

Your view needs to return a HttpResponse object but your code doesn't.

@api_view(['GET', ])
def api_detail_educationlevel(request):
    try:
        education = EducationLevel.objects.all()
    except EducationLevel.DoesNotExist:
        return Response(status=status.HTTP_400_BAD_REQUEST)
    if request.method == "GET":
        serializer = EducationLevelSerializer(education)
        return Response(serializer.data)

The if statement should be written outside the except block. Small issue easy to overlook.

The above code is also somewhat redundant. The decorator @api_view(['GET']) will anyway check if request.method has value GET or not. So, you can remove the redundant check. The resulting code will become:

@api_view(['GET', ])
def api_detail_educationlevel(request):
    try:
        education = EducationLevel.objects.all()
    except EducationLevel.DoesNotExist:
        return Response(status=status.HTTP_400_BAD_REQUEST)
    serializer = EducationLevelSerializer(education)
    return Response(serializer.data)

Edit: As pointed out by @youngminz you don't need to do exception handling if you are getting all objects without any filter conditions.

Compro Prasad
  • 162
  • 1
  • 14
0

The reason for this is your Except block. In your response, you have not passed any data but just status. I would suggest passing an empty string as data with status

Arvind Kumar
  • 913
  • 10
  • 23
  • how do i do that? even though i have this ? "" education = EducationLevel.objects.all() "" –  Jun 15 '20 at 14:36
0

The error you are talking about is when the return value of the view is None.

@api_view(['GET', ])
def api_detail_educationlevel(request):

  try:
    education = EducationLevel.objects.all()

The above code EducationLevel.objects.all() never raises EducationLevel.DoesNotExist exception, as the Django Queryset does lazy evaluation. So, the following exception handling process is not executed.

  except EducationLevel.DoesNotExist:
    return Response(status=status.HTTP_400_BAD_REQUEST)

    if request.method == "GET":
      serializer = EducationLevelSerializer(education)
      return Response(serializer.data)

... In python, if function doesn't specify a return value, it returns None. This is why Expected a Response, HttpResponse or HttpStreamingResponse to be returned from the view, but received a <class 'NoneType'> error is raisen.

youngminz
  • 1,364
  • 2
  • 14
  • 23
0

You have to check all your conditions so that all of them are "Responsed"

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 23 '23 at 18:41