Vaibhav Mule
Vaibhav Mule

Reputation: 5136

Django FormView vs CreateView

What is difference between Django FormView and CreateView?

Only diffrence I see, FormView require ModelForm but CreateView doesn't.

Otherwise both does same thing creating an object.

Upvotes: 27

Views: 7207

Answers (1)

v1k45
v1k45

Reputation: 8250

From Django-docs:

FormView:

A view that displays a form. On error, redisplays the form with validation errors; on success, redirects to a new URL.

It can be used for various purposes and is not restricted to creating objects. A nice example would be using it as a contact form and sending emails without creating records in database.

CreateView:

A view that displays a form for creating an object, redisplaying the form with validation errors (if there are any) and saving the object.

The sole purpose of this generic view is to create objects. But it is not limited to creating objects. You can send emails from this view too (just like FormView)

If your FormView creates model objects, it is best to use CreateView and not creating a modelform, that's what generic views are for, reducing repetition.

Upvotes: 23

Related Questions