Django属性错误 - 表单对象没有属性is_valid错误

时间:2017-06-21 22:00:04

标签: python django django-forms

我一直收到错误,说我的BundleForm没有属性is_valid。我进行了双重和三重检查,我的BundleForm与模型相关联,特别是我的Bundle模型。我无法弄清楚为什么我会收到这个错误。任何评论/意见都非常欢迎!

models.py

class Bundle(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
user_profile = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
name = models.CharField(max_length=100)

def __str__(self):
    return self.name

forms.py

class BundleForm(forms.ModelForm):
    name = forms.CharField(
        help_text='Enter mission name. Change to hidden unless mouse over. ',
        initial='bundle_name',
        required=True,
        max_length=50,
    )

    class Meta:
        model = Bundle             
        fields = ('name',)  

views.py

def new_bundle(request):
form1 = BundleForm()
if request.method == 'POST':
    form1 = BundleForm(request.POST)
    if form1.is_vaild():
        bundle = form1.save(commit=True)
        return render(request, 'build_a_bundle/new_bundle.html', {'bundle':bundle})
    else:
        return HttpResponse('Form 1 Error')    
return render(request, 'build_a_bundle/new_bundle.html', {'form1':form1})

1 个答案:

答案 0 :(得分:1)

语法错误,将if form1.is_valid():更改为:{{1}}。