Class Based Views

CBV – CreateView

From Classy Class Based Views CreateView

View for creating a new object, with a response rendered by a template.


Three attributes are required to get the template to render. Two we’ve seen before (queryset and template_name). The new one we haven’t see before is the fields attribute.

  • fields: specifies what fields from the model or queryset will be displayed on the rendered template. You can you set fields to __all__ if you want to return all of the fields


queryset = Person.objects.all()
fields = '__all__'
template_name = 'rango/person_form.html'

path('create_view/', views.myCreateView.as_view(), name='create_view'),


{% extends 'base.html' %}

    {% block title %}
        {{ title }}
    {% endblock %}

{% block content %}
    <h3>{{ type }} View</h3>
    <form action="." method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">SUBMIT</button>
{% endblock %}


A visual representation of how CreateView is derived can be seen here:


A simple way to implement a form to create items for a model. We’ve completed step 1 for a basic CRUD application.

One reply on “CBV – CreateView”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.