Skip to content
Advertisement

The view home.views.login_view didn’t return an HttpResponse object. It returned None instead

I am trying to create role based login system but it continuously showing the mentioned error N.B: I’m new to django

def login_view(request):
    form = AuthenticationForm()
    if request.method=='POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        user = authenticate(username=username, password=password)
        if user is not None and user.is_student==True:
            login(request,user)
            if request.GET.get('next'):
                return render(request.GET.get('next'))
            else:
                return redirect(homepage) 

Advertisement

Answer

Every view in django should return an instance of HttpResponse object, therefore you need to cover all edge cases, e.g.:

def login_view(request):
    form = AuthenticationForm()

    if request.method=='POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        user = authenticate(username=username, password=password)

        if user is not None and user.is_student==True:
            ...
        # Return 401 if the user is None or the user is not a student
        return HttpResponse('Unauthorized', status=401)

    # Default HttpResponse on `GET` requests
    return render(request, '<your_template.html>', {'form': form})
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement