下面是表格:
通过为某些表单字段发送空值来努力验证表单。
姓名和电话是必填字段。当用户键入并单击提交时,应将所有值连接起来并保存在文件中。
表格.py
from django import forms
class ReviewForm1(forms.Form):
PRECAUTIONS_CHOICES = (
('no', 'No'),
('yes', 'Yes')
)
UNIT1_CHOICES = (
('Very Satisfied', 'Very Satisfied'),
('Satisfied', 'Satisfied'),
('neutral', 'neutral'),
('Unsatisfied', 'Unsatisfied'),
('Very Unsatisfied', 'Very Unsatisfied'),
)
name = forms.CharField(required=True, widget=forms.TextInput(attrs={'required': 'true'}))
phone = forms.CharField(required=False)
email = forms.EmailField(required=True, widget=forms.TextInput(attrs={'required': 'true'}))
precautions = forms.ChoiceField(required=True, choices= PRECAUTIONS_CHOICES, widget=forms.Select())
unit1_ambience = forms.ChoiceField(required=True, choices= UNIT1_CHOICES, widget=forms.Select())
review = forms.CharField(required=False, widget=forms.Textarea())
def clean(self):
name = self.cleaned_data.get('name')
phone = self.cleaned_data.get('phone')
review = self.cleaned_data.get('review')
email = self.cleaned_data.get('email')
precautions = self.cleaned_data.get('precautions')
unit1_ambience = self.cleaned_data.get('unit1_ambience')
expected_string = ''
options = [name,phone,review,email,precautions,unit1_ambience]
for option in options:
if not option:
continue
else:
expected_string = expected_string + option + '\n'
# concat all with review and then return string
return expected_string
以下是单击保存时出现的错误:
赋值前引用的局部变量“review”
我认为 form.is_valid() 返回 false,如何解决这个问题?
宝慕林4294392
相关分类