[Django]-View didn't return an HttpResponse object. It returned None instead

5👍

You are redirecting if form.is_valid() but what about the form is invalid? There isn’t any code that get’s executed in this case? There’s no code for that. When a function doesn’t explicitly return a value, a caller that expects a return value is given None. Hence the error.

You could try something like this:

def edit(request, row_id):
    rating = get_object_or_404(Rating, pk=row_id)
    context = {'form': rating}
    if request.method == "POST":
        form = RatingForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('home.html')
        else :
            return render(request, 'ratings/entry_def.html', 
                          {'form': form})
    else:
        return render(
            request,
            'ratings/entry_def.html',
            context
        )

This will result in the form being displayed again to the user and if you have coded your template correctly it will show which fields are invalid.

👤e4c5

3👍

I was with this same error, believe it or not, was the indentation of Python.

0👍

your error to the indentation of a Python file. You have to be careful when following tutorials and/or copy-pasting code. Incorrect indentation can waste lot of valuable time.

👤sith91

0👍

You Should Return What file you are rendering instead of Direct Render.

def index(request):
    return render(request, 'index.html')

def login(request):
    return render(request,'login.html')

def logout(request): 
    return render(request,'index.html')

Leave a comment