如何在/-处修复ValueError视图todo.views.home没有返回HttpResponse对象。它返回了None

时间:2019-03-30 19:28:51

标签: python-3.x django-2.1

我正在使用Django开发待办事项列表应用程序,但我在哪里出错呢?

我尝试通过如下形式放置if form.is_valid的else块-

else:
     return HttpResponse("Form is invalid")

此后,以下错误消失了,但是它显示了我在HttpResponse中的味精。表单如何无效?

views.py-

from django.shortcuts import render, redirect
from .models import List
from .forms import ListForm
from django.contrib import messages
from django.http import HttpResponse

# Create your views here.

def home(request):
    if request.method == 'POST':
        form = ListForm(request.POST or None)

        if form.is_valid():
            form.save()
            all_items = List.objects.all
            messages.success(request, ('Item has been Added to the List!'))
            return render(request,'home.html', {'all_items' : all_items})

    else:
        all_items = List.objects.all
        return render(request,'home.html', {'all_items' : all_items})

forms.py-

from django import forms
from .models import List

class ListForm(forms.ModelForm):
    class Meta:
        model = List
        fields = ["item", "completed"]

models.py-

from django.db import models

# Create your models here.
class List(models.Model):
    item = models.CharField(max_length=200)
    completed = models.BooleanField(default=False)


    def __str__(self):
        return self.item + ' | ' + str(self.completed)

这是我的错误消息:

  

ValueError位于/-视图todo.views.home没有返回HttpResponse对象。它返回None。

0 个答案:

没有答案