KeyError on Form using datePicker

15 views
Skip to first unread message

Baktha Muralidharan

unread,
Feb 21, 2017, 12:01:23 PM2/21/17
to Django users
Hello,

I am new to Django framework and have run into the following issue.  Any help to resolve the issue will be much appreciated.

Background:
I am using the jQuery datePicker to gather date input. I am able to see the calendar show on the Appointment form and am able to select a date from the calendar. However, once submitted, I am not able to extract the selected date, in the post processing function.

I have read somewhere about form field not being extractable if user did not enter a value. In my scenario, I don't manually enter "perfDate", but rather made a selection on the calendar. I get KeyError on trying to read the field value. The Django debugs show that the POST data as shown here-

POST

VariableValue
csrfmiddlewaretoken
'C4fvNKcVLaFpSCGHcukNhEIFkqLyCPqkgXCc64R8JNjQwq5EST3KCPRkMOZpAslT'
perfDate
'02/25/2017'

 

Following are my form, template and views files:

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})


Thanks,
/Baktha Muralidharan
Reply all
Reply to author
Forward
0 new messages