我一直收到错误,说我的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})
答案 0 :(得分:1)
语法错误,将if form1.is_valid():
更改为:{{1}}。