与jQuery datepicker一起使用时,尝试读取表单字段值throwns keyerror

时间:2017-02-21 20:27:54

标签: javascript jquery python django datepicker

我是Django框架的新手,遇到了以下问题。任何有助于解决问题的帮助都将不胜感激。

背景:

我正在使用jQuery datePicker来收集日期输入。我能够在(约会)表单上看到日历,并从日历中选择日期。但是,一旦提交,尝试提取所选日期,在后处理功能中,会抛出一个关键错误。

我不知道这是否与我没有手动输入" perfDate"有关,而是在日历上做出选择。 Django调试显示POST数据如下所示 -

enter image description here

以下是我的表单,模板和视图文件:

forms.py:

class DateInput(forms.DateInput):
    input_type = 'date'

class AppointmentForm(forms.Form):
    class Meta:
        model = Appointment
        widgets = {
            'perfDate': forms.DateInput(attrs={'class':'datepicker'}),
        }

datePicker.html:

<!doctype html>
<html lang="en">
<head>
... JS code...
</head>
    <body>
        <form action="/users/makeReservation/" method="post">
            {% csrf_token %}
            <!--{{ form.date }} -->
            <p>Desired date:<p>
        <input name="perfDate" type="text" id="id_date"></p>
            <!-- The rest of my form -->
            <input type="submit" value="Reserve" />
        </form>
    </body>

views.py:

def makeReservation(request):
    if request.method == 'POST':
        aptForm = AppointmentForm(request.POST)
        # Have we been provided with a valid form?
        if aptForm.is_valid():
            aptDate = aptForm.cleaned_data['perfDate'])
            ...
        else:
            # The supplied form contained errors - just print them to the terminal.
            print(form.errors)
    else:
        aptForm = AppointmentForm(request.POST)
        return render(request, 'datePicker.html', {'form' : aptForm})

谢谢, / Baktha Muralidharan

0 个答案:

没有答案