Logic1
Logic1

Reputation: 1847

django forms, cleaned_data is empty

I have been playing around with forms a little and cant seem to understand why cleaned_data is not giving me any usable output (aka the dict appears to be completely empty). What id like to do is have a form on a page with two date selector so the user can select a from and to date that Django will then query a database that has periodic thermocouple measurements and create a table.

views.py

def temperature_data(request):
    date_select_form = CalLabDateSelect(request.POST)

    if request.method == 'POST':        
        if date_select_form.is_valid(): # All validation rules pass
            print "this should be some date/time data from date_select_form:", date_select_form.cleaned_data
            #return HttpResponseRedirect('/test_page/') # Redirect after POST

    raw_data = Callab.objects.all().using('devices').order_by('-time')
    return render_to_response("temperature_display.html",
                              locals(),
                              context_instance=RequestContext(request))

forms.py

def make_custom_datefield(f):
    formfield = f.formfield()
    if isinstance(f, models.DateField):
        formfield.widget.format = '%m/%d/%Y'
        formfield.widget.attrs.update({'class':'datePicker', 'readonly':'true'})
    return formfield

class CalLabDateSelect(forms.Form):
    formfield_callback = make_custom_datefield

when i visit the page and select a date then submit the form i see this outputted to the console:

QueryDict: {u'date': [u'10/04/2014'], u'csrfmiddlewaretoken': [u'C5PPlMU3asdFwyma9azFDs4DN33CMmvK']}
this should be some date/time data from date_select_form: {}

all i notice is that the dictionary is empty {} but the request.POST data shows 10/04/2014???

any ideas why this is happening??

And thank you all very much for any help in understand this!!

Upvotes: 1

Views: 1879

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599530

Your form doesn't actually define any fields, so I don't know what you're expecting to get in cleaned_data. formfield_callback is only useful in a ModelForm, where it operates on the fields already defined by a model: but your form is not based on a model.

Either use a model form, or define your form fields explicitly in your form class.

Upvotes: 2

Related Questions