Skip to content
Advertisement

Django – CreateView specifying a form to use from forms.py

I’m trying to use a form I have defined in forms.py within my CreateView, as I have css classes and field labels I want to apply.

Documentation states declaring a form_class field within your CreateView, but trying to set it as my ModelForm gives me the error TypeError ‘AssessmentForm’ object is not callable

JavaScript

What is the proper implementation of setting a ClassBasedView to use a ModelForm?

Advertisement

Answer

The reason you are getting this error is that you are calling this AssessmentForm(). So instead of using this

JavaScript

use this:

JavaScript

And if you deep dive into CreateView you came to know that this class is using FormMixin that has a method name get_form, with the implementation something like this.

JavaScript

and get_form_class() simply returns your self.form_class, something like this.

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement