Django form error

django, forms

Solution

Your form is definitely not bound. Read about Bound and Unbound forms.

From that documentation:

To bind data to a form, pass the data as a dictionary as the first parameter to your Form class constructor.

That means that also the change of a field in your model doesn't make the form bound. You have to pass those values explicitly via the constructor. But:

Note that passing an empty dictionary creates a bound form with empty data

and consider this

If you have a bound Form instance and want to change the data somehow, or if you want to bind an unbound Form instance to some data, create another Form instance. There is no way to change data in a Form instance. Once a Form instance has been created, you should consider its data immutable, whether it has data or not.

If you validate an unbound form:

It's meaningless to validate a form with no data, but, for the record, here's what happens with unbound forms:

`>>> f = ContactForm()` `>>> f.is_valid()` `False` `>>> f.errors {}`

Problem

``` class JobForm(forms.ModelForm): class Meta: model = models.Job ``` That was my form, now trying to save it will raise an exception, and trying to validate it just fails with no errors.... ``` job = get_object_or_404(models.Job, pk=1) form = forms.JobForm(instance = job) try: form.save() except: print sys.exc_info() #=>(<type 'exceptions.AttributeError'>, AttributeError("'JobForm' object has no attribute 'cleaned_data'",), <traceback object at 0x1029dbb48>) ``` Tried to validate it: ``` if form.is_valid(): form.save() else: print 'error' print form.errors, len(form.errors) #=> 'error' #=> 0 ``` So the form isn't valid, but the there are no errors! Any idea?

Original source