我创建了一个表单和一个视图,用于检查用户输入lesson_instrument
和lesson_level
是否与用户对象的值instrument1
和level1
相匹配。如果lesson_instrument == instrument1
和level1 >= lesson_level
可以通过该表格。但是,无论我在表单中输入的内容如何,表单始终以有效方式传递,即使函数不满足要求,表单也不会失败。对于解决此问题的任何帮助,我将不胜感激,因为我似乎无法找出问题所在。
forms.py
class LessonForm(forms.ModelForm):
lesson_instrument = forms.ChoiceField(choices=instrument_list, widget=forms.Select(attrs={'class' : 'form-control', 'required' : 'True'}))
lesson_level = forms.ChoiceField(choices=level_list, widget=forms.Select(attrs={'class' : 'form-control', 'required' : 'True'}))
lesson_length = forms.ChoiceField(choices=length_list, widget=forms.Select(attrs={'class' : 'form-control', 'required' : 'True'}))
lesson_day = forms.ChoiceField(choices=day_list, widget=forms.Select(attrs={'class' : 'form-control', 'required' : 'True'}))
lesson_time = forms.ChoiceField(choices=time_list, widget=forms.Select(attrs={'class' : 'form-control', 'required' : 'True'}))
lesson_weekly = forms.BooleanField(required=False)
class Meta:
model = Lesson
fields = ('lesson_instrument', 'lesson_level', 'lesson_length', 'lesson_day', 'lesson_time', 'lesson_weekly')
def lesson_allowed(self):
# Check that the user is allowed to teach that instrument and level
lesson_instrument = self.cleaned_data.get("lesson_instrument")
lesson_level = self.cleaned_data.get("lesson_level")
if lesson_instrument == user.instrument1:
level1 = user.level1
if lesson_level == "Beginner":
lesson_level = 1
if level1 == 'Beginner':
level1 = 1
if level1 >= lesson_level:
return lesson_instrument
return lesson_level
if level1 == 'Advanced':
level1 = 2
if level1 >= lesson_level:
return lesson_instrument
return lesson_level
if level1 == 'Intermediate':
level1 = 3
if level1 >= lesson_level:
return lesson_instrument
return lesson_level
if lesson_level == "Advanced":
lesson_level = 2
if level1 == 'Beginner':
level1 = 1
if level1 >= lesson_level:
return lesson_instrument
return lesson_level
if level1 == 'Advanced':
level1 = 2
if level1 >= lesson_level:
return lesson_instrument
return lesson_level
if level1 == 'Intermediate':
level1 = 3
if level1 >= lesson_level:
return lesson_instrument
return lesson_level
if lesson_level == "Intermediate":
lesson_level = 3
if level1 == 'Beginner':
level1 = 1
if level1 >= lesson_level:
return lesson_instrument
return lesson_level
if level1 == 'Advanced':
level1 = 2
if level1 >= lesson_level:
return lesson_instrument
return lesson_level
if level1 == 'Intermediate':
level1 = 3
if level1 >= lesson_level:
return lesson_instrument
return lesson_level
views.py
def new_lesson(request):
if request.method == 'POST':
form = LessonForm(request.POST)
if form.is_valid():
lessons = form.save(commit=False)
lessons.user = request.user
lessons.save()
messages.success(request,'Lesson successfully created')
return redirect('/teacher/schedule')
else:
messages.error(request, 'Information entered was invalid')
else:
form = LessonForm()
lessons = Lesson.objects.filter(user=request.user)
form = LessonForm(request.POST)
context = {'form' : form, 'lessons': lessons}
return render(request, 'teacher/new_lesson.html', context)
答案 0 :(得分:2)
对于自定义验证,您应该为表单定义一个自定义clean()方法。参见this section in the Django docs。看来这是您尝试使用lesson_allowed
方法执行的操作,但是应将其命名为clean()
,如果未通过则抛出forms.ValidationError()
。