Somehow does not work when I remove a simple print

somehow when I just remove the print without touching anything else the code starts throwing an error when I return the print back, it magically works. Client side send the same data all the time so it’s likely not a client side problem

essay_data is a dictionary

    form = FormClass(essay_data)

    print(form)

    essay_data = form.cleaned_data

the change where it throws an error


    form = FormClass(essay_data)

    # print(form)

    essay_data = form.cleaned_data

the error

Traceback (most recent call last):
File “[redacted]/.venv/lib/python3.13/site-packages/django/core/handlers/exception.py”, line 55, in inner
response = get_response(request)
File “[redacted]/.venv/lib/python3.13/site-packages/django/core/handlers/base.py”, line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File “[redacted]/views.py”, line 929, in [redacted]
essay_data = form.cleaned_data
^^^^^^^^^^^^^^^^^
AttributeError: ‘DynamicForm’ object has no attribute ‘cleaned_data’. Did you mean: ‘changed_data’?

Creating an instance of a form immediately followed by a reference to a cleaned_data attribute is not going to work.

The cleaned_data attribute is not created until the form is cleaned.

Printing the form forces the clean process to run - that’s why it causes the following statement to work in your case.

Quoting directly from the docs:

Once you’ve created a Form instance with a set of data and validated it, you can access the clean data via its cleaned_data attribute:

[Emphasis added.]

See the example at The Forms API | Django documentation | Django. Notice how the example calls the is_valid method before trying to access that attribute. The is_valid method also calls the cleaning process on the form.

1 Like