表格如下:
发送一些表单字段的空值来验证表单很困难。你知道吗
姓名和电话是必填字段。当用户键入并单击submit时,所有值都应该连接起来并保存到文件中。你知道吗
表单.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
视图.py代码段
@require_POST
@csrf_protect
def revfile_save(request):
"""
This page processes and saves review file.
"""
form = ReviewForm1(request.POST)
if form.is_valid():
review = form.cleaned_data
reviewfile_name = "/root/sjfacweb/" + remote.sjfacweb()
#content writes to timestamped filename
remote.write_reviewfile(reviewfile_name,False,review)
以下是单击“保存”时出现的错误:
赋值前引用的局部变量“review”
我想表格.u有效吗()返回false,如何解决此问题?你知道吗
谢谢
这与“空字段”无关。该视图的其余部分应该在
is_valid
块中,当表单无效时,您应该执行其他操作-即使用无效表单重新呈现模板。你知道吗请注意,
clean()
应该以dict而不是连接字符串的形式返回清理过的数据;连接应该在其他地方完成。你知道吗相关问题 更多 >
编程相关推荐