Reputation: 2608
I'm trying to make this simple code to work:
class TestForm(forms.Form):
start_date = forms.DateField(widget=SelectDateWidget())
end_date = forms.DateField(widget=SelectDateWidget())
def test(request):
form = TestForm(request.GET)
if form.is_valid():
start = request.GET.get('start_date')
end = request.GET.get('end_date')
test_var = 'Hello'
return render(request, 'schedule/test.html', locals())
{% extends "base.html" %}
{% block content %}
<form method="get">
<table>
{{ form.as_table }}
<tr><td><input type="submit"></td><td></td></tr>
</table>
</form>
<br/>
<b>Start date</b>: {{ start }}<br />
<b>End date</b>: {{ end }}<br />
<b>Test var:</b> {{ test_var }}
{% endblock %}
It looks that SelectDateWidget does not set {{ start }} and {{ end }} variables correctly:
What am I doing wrong?
Upvotes: 1
Views: 873
Reputation: 22449
form values are accessed via cleaned data, try:
start = form.cleaned_data.get('start_date')
end = form.cleaned_data.get('end_date')
if you want the get parameters they are passed by day, month and year:
print: request.GET will give you:
<QueryDict: {u'end_date_day': [u'4'], u'start_date_day': [u'1'], u'end_date_year': [u'2012'], u'end_date_month': [u'3'], u'start_date_month': [u'2'], u'start_date_year': [u'2012']}>
Upvotes: 3