django 错误:视图未返回HttpResponse对象

okxuctiv  于 2022-12-20  发布在  Go
关注(0)|答案(1)|浏览(137)

当我点击链接到上述视图的按钮时,发生错误
以下是我看法

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})

错误:
视图companies. views. change_company没有返回HttpResponse对象,而是返回None。

zzlelutf

zzlelutf1#

将您的函数更改为:

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})

相关问题