Ниже приведена форма:
пытается проверить форму с отправкой пустых значений для некоторых полей формы.
Имя ителефон был обязательным полем.Когда пользователь вводит и нажимает кнопку «Отправить», все значения должны быть объединены и сохранены в файле.
forms.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
views.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)
Ниже приведена ошибка, которую я получаю, когда нажимаю сохранить:
локальная переменная 'обзор', на которую ссылается перед назначением
Iдумаю, form.is_valid () возвращает false, как решить эту проблему?
Спасибо