27

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.

1 Answer 1

23

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.

5
  • 2
    Don't agree with best choice, for example most of time we don't want to display all model fields.
    – sonus21
    Commented Jul 7, 2016 at 7:05
  • 3
    then you can just include only the model fields you want to display using the fields attribute.
    – v1k45
    Commented Jul 7, 2016 at 7:53
  • What about having additional field(s) , for example combination of two models, there are some others use case as well.
    – sonus21
    Commented Jul 7, 2016 at 8:03
  • 1
    then you'll have to create a modelform. I clearly mentioned that you can use createview without modelform for generically creating objects. If you want to do something which is not done by createview, you can just create a modelform and override other methods of the view.
    – v1k45
    Commented Jul 7, 2016 at 8:37
  • Yeah that's correct, but in my opinion one works best over another based on the use case ;)
    – sonus21
    Commented Jul 7, 2016 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.