表格未验证提交的数据

时间:2019-03-02 22:36:02

标签: python django forms

在我看来,我包含的代码应检查lesson_instrument的表单输入是否等于用户的instrument1,instrument2,instrument3,instrument4或instrument5值。如果是这样,则表单应该通过,否则,则应该失败。但是,即使lesson_instrument不等于用户的乐器,该表格似乎仍在通过。如果能理解为什么会发生这种情况,我将不胜感激。

HTML

<form action="{% url 'teacher:new_lesson' %}" method="POST" autocomplete="off">
            {% csrf_token %}

                <!-- Instrument -->
                <div class="form-group">
                    <label>Instrument</label>
                    {{ form.lesson_instrument }}
                </div>

                <!-- Date Time -->
                <div class="form-group">
                    <label>Lesson time availability on one date</label>
                    {{ form.lesson_datetime_start }}
                    {{ form.lesson_datetime_end }}
                </div>

                <!-- Check Boxes -->
                <div class="checkbox">
                    {{ form.lesson_weekly }}
                    <label>Create lesson weekly</label>
                </div>

                <div class="bottom">
                    <button type="submit" name="submit" class="btn blue_button">Save</button>
                </div>

        </form>

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_datetime_start = forms.DateTimeField(input_formats=['%Y-%m-%d %I:%M %p'], widget=forms.DateTimeInput(attrs={'class': 'form-control', 'placeholder':'YYYY-MM-DD Hour:Minute am/pm'}))
    lesson_datetime_end = forms.DateTimeField(input_formats=['%Y-%m-%d %I:%M %p'], required=False, widget=forms.DateTimeInput(attrs={'class': 'form-control', 'placeholder':'YYYY-MM-DD Hour:Minute am/pm'}))
    lesson_weekly = forms.BooleanField(required=False)

    class Meta:
        model = Lesson
        fields = ('lesson_instrument', 'lesson_datetime_start', 'lesson_datetime_end', 'lesson_weekly')

views.py

def new_lesson(request):
    if request.user.is_authenticated and request.user.time_zone:
        activate(request.user.time_zone)
    else:
        deactivate()

    user = request.user

    if request.method == 'POST':
        form = LessonForm(request.POST)

        if form.is_valid():
            data = form.cleaned_data
            field = data['lesson_instrument']
            if field == user.instrument1 or user.instrument2 or user.instrument3 or user.instrument4 or user.instrument5:
                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, 'Not verified to teach that instrument')

        else:
            messages.error(request, 'Information entered was invalid')

    else:
        form = LessonForm()

    form = LessonForm()
    lessons = Lesson.objects.filter(user=request.user)
    context = {'form' : form, 'lessons': lessons}
    return render(request, 'teacher/new_lesson.html', context)

0 个答案:

没有答案