views:

248

answers:

1

I am trying to come up with a form that lets the user select a date range to generate a web query in Django. I am having errors getting the date to filter with in my view, I am unable to strip the date.

Here is my forms.py

class ReportFiltersForm(forms.Form):
start_date = forms.DateField(input_formats='%Y,%m,%d',widget=SelectDateWidget())
end_date = forms.DateField(input_formats='%Y,%m,%d',widget=SelectDateWidget())

And my view

if request.method == 'POST':
form = ReportFiltersForm(request.POST)
sdy = request.POST['start_date_year']
sdm = request.POST['start_date_month']
sdd = request.POST['start_date_day']
edy = request.POST['end_date_year']
edm = request.POST['end_date_month']
edd = request.POST['end_date_day']
start_date= datetime.date(sdy, sdm, sdd)
end_date= datetime.date(edy, edm,edd)

Traceback

Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/django/core/servers/basehttp.py", line 651, in __call__
return self.application(environ, start_response)
File "/usr/lib/python2.6/site-packages/django/core/handlers/wsgi.py", line 241, in __call__
response = self.get_response(request)
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py", line 134, in get_response
return self.handle_uncaught_exception(request, resolver, exc_info)
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py", line 154, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py", line 92, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/home/projects/acms/cms/views.py", line 470, in eventreports
start_date= datetime.date(sdy, sdm, sdd)
TypeError: an integer is required
A: 

There are many mistakes you are making here.

First off, to fix your TypeError, you need to cast your data into int like so:

...
sdy = int(request.POST['start_date_year'])
#Do the same with the other 5 fields

However, this is a really bad way of doing things. For one, you will have to put try/except blocks around each field to make sure the user entered an integer and not a string.

Since you are already using Django Forms, why not let it do all the work for you? Here is how you would do this:

if request.method == 'POST':
    form = ReportFiltersForm(request.POST)
    if form.is_valid():
        start_date = form.cleaned_data['start_date']
        end_date = form.cleaned_data['end_date']
        #Do whatever you need to do with this data now. Since you had
        #defined the fields as DateFields, you will automatically get
        #datetime.date objects.
    else:
        return render_to_response('mytemplate.html', {'form' : form})
....

This is a much better way to do things for a couple of reasons. Using this way, you can let Django take care of validating the user input for you. Additionally, it will also cast this data into datetime.date objects. Finally, it will allow you to give better error messages to your user if they entered invalid data.

Rishabh Manocha