Error: view didn't return an HttpResponse object

The error occurs when I click button which is linked to the mentioned view

here is my view

def change_company(request,pk):
    item = get_object_or_404(Companies, pk=pk)

    if request.method == "POST":
        form = CompaniesForm(request.POST, instance=item)
        if form.is_valid():
            form.save()
            return redirect('display_companies')
    else:
        form = CompaniesForm(instance=item)
        return render(request, 'change_company.html', {'form':form})

error:

The view companies.views.change_company didn't return an HttpResponse object. It returned None instead.

Change your function to:

def change_company(request,pk):
    item = get_object_or_404(Companies, pk=pk)
    form = CompaniesForm(instance=item)

    if request.method == "POST":
        form = CompaniesForm(request.POST, instance=item)
        if form.is_valid():
            form.save()
            return redirect('display_companies')
    
    return render(request, 'change_company.html', {'form':form})
Back to Top