Tuesday, February 21, 2017

KeyError on Form using datePicker

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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/86157664-4140-4304-a9ab-f5bae56848d5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment