{"id":5980,"date":"2022-12-01T03:59:33","date_gmt":"2022-12-01T03:59:33","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=5980"},"modified":"2022-12-03T08:06:51","modified_gmt":"2022-12-03T08:06:51","slug":"django-createview","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/django-tutorial\/django-createview\/","title":{"rendered":"Django CreateView"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Django <code>CreateView<\/code> class to define a class-based view that creates a task for the Todo application.<\/p>\n\n\n\n<p class=\"note\">This tutorial begins where&nbsp;the&nbsp;<a href=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-detailview\/\">Django DetailView tutorial<\/a>&nbsp;left off.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='defining-the-createview-class'>Defining the CreateView class <a href=\"#defining-the-createview-class\" class=\"anchor\" id=\"defining-the-createview-class\" title=\"Anchor for Defining the CreateView class\">#<\/a><\/h2>\n\n\n\n<p>The <code>CreateView<\/code> class allows you to create a class-based view that displays a form for creating an object, redisplaying the form with validation errors, and saving the object into the database.<\/p>\n\n\n\n<p>To use the <code>CreateView<\/code> class, you define a class that <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-inheritance\/\">inherits from it<\/a> and add some attributes and methods.<\/p>\n\n\n\n<p>For example, the following uses the <code>CreateView<\/code> class to define a class-based view that renders a form for creating a new task in the <a href=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-todo-app\/\">Todo app<\/a>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># ..<\/span>\n<span class=\"hljs-keyword\">from<\/span> django.views.generic.edit <span class=\"hljs-keyword\">import<\/span> CreateView\n<span class=\"hljs-keyword\">from<\/span> django.contrib <span class=\"hljs-keyword\">import<\/span> messages\n<span class=\"hljs-keyword\">from<\/span> django.urls <span class=\"hljs-keyword\">import<\/span> reverse_lazy\n<span class=\"hljs-keyword\">from<\/span> .models <span class=\"hljs-keyword\">import<\/span> Task\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">TaskCreate<\/span><span class=\"hljs-params\">(CreateView)<\/span>:<\/span>\n    model = Task\n    fields = &#91;<span class=\"hljs-string\">'title'<\/span>,<span class=\"hljs-string\">'description'<\/span>,<span class=\"hljs-string\">'completed'<\/span>]\n    success_url = reverse_lazy(<span class=\"hljs-string\">'tasks'<\/span>)\n    \n   \n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">form_valid<\/span><span class=\"hljs-params\">(self, form)<\/span>:<\/span>\n        form.instance.user = self.request.user\n        messages.success(self.request, <span class=\"hljs-string\">\"The task was created successfully.\"<\/span>)\n        <span class=\"hljs-keyword\">return<\/span> super(TaskCreate,self).form_valid(form)\n\n<span class=\"hljs-comment\"># other classes &amp; functions<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works:<\/p>\n\n\n\n<p>First, import the <code>CreateView<\/code> class, the <code>reverse_lazy()<\/code> function, and <code>messages<\/code> module.<\/p>\n\n\n\n<p>Second, define the <code>TaskCreate<\/code> class that inherits from the <code>CreateView<\/code> class. In the <code>CreateView<\/code> class, we define the following attributes and methods:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>model<\/code> specifies the class of the object to be created (<code>Task<\/code>).<\/li>\n\n\n\n<li><code>fields<\/code> is a list of fields that display on the form. In this example, the form will display title, description, and completed attributes of the <code>Task<\/code> model.<\/li>\n\n\n\n<li><code>success_url<\/code> is the target URL that Django will redirect to once a task is created successfully. In this example, we redirect to the task list using the <code><code>reverse_lazy()<\/code><\/code> function. The <code><code>reverse_lazy()<\/code><\/code> accepts a view name and returns an URL.<\/li>\n\n\n\n<li><code>form_valid()<\/code> &#8211;  is a method called once the form is posted successfully. In this example, we set the user to the currently logged user, <a href=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-flash-messages\/\">create a flash message<\/a>, and return the result of the <code>form_valid()<\/code> method of the superclass.<\/li>\n<\/ul>\n\n\n\n<p>By default, the <code>CreateView<\/code> class uses the <code>task_form.html<\/code> template from the <code>templates\/todo<\/code> with the following naming convention:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">model_form.html<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you want to use a different template, you can override the default template using the <code>template_name<\/code> attribute in the <code>TaskCreate<\/code> class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='creating-the-task_form-html-template'>Creating the task_form.html template <a href=\"#creating-the-task_form-html-template\" class=\"anchor\" id=\"creating-the-task_form-html-template\" title=\"Anchor for Creating the task_form.html template\">#<\/a><\/h2>\n\n\n\n<p>Create the <code>task_form.html<\/code> in the <code>templates\/todo<\/code> directory with the following code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">{%extends 'base.html'%}\n\n{%block content%}\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"center\"<\/span>&gt;<\/span>\n\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">form<\/span> <span class=\"hljs-attr\">method<\/span>=<span class=\"hljs-string\">\"post\"<\/span> <span class=\"hljs-attr\">novalidate<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"card\"<\/span>&gt;<\/span>\n\t \t{%csrf_token %}\n\t \t\n\t \t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h2<\/span>&gt;<\/span>Create Task<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h2<\/span>&gt;<\/span>\n\t\t{% for field in form %}\n\t\t\t{% if field.name == 'completed' %}\n\t\t\t\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n\t\t\t\t\t{{ field.label_tag }}\n\t\t\t\t\t{{ field }}\n\t\t\t\t<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n\t\t\t\t{% if field.errors %}\n\t        \t\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">small<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"error\"<\/span>&gt;<\/span>{{ field.errors|striptags  }}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">small<\/span>&gt;<\/span> \n\t        \t{% endif %}\n\t\t\t{% else %}\n\t    \t\t{{ field.label_tag }} \n\t        \t{{ field }}\n\t        \t{% if field.errors %}\n\t        \t\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">small<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"error\"<\/span>&gt;<\/span>{{ field.errors|striptags  }}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">small<\/span>&gt;<\/span> \n\t        \t{% endif %}\n\t        {% endif %}\n\t\t{% endfor %}\n\t\t\n\t\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"form-buttons\"<\/span>&gt;<\/span>\n\t\t\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"submit\"<\/span> <span class=\"hljs-attr\">value<\/span>=<span class=\"hljs-string\">\"Save\"<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"btn btn-primary\"<\/span>\/&gt;<\/span>\n\t\t\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"{%url 'tasks'%}\"<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"btn btn-outline\"<\/span>&gt;<\/span>Cancel<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span>\n\t\t<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\t<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">form<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\n{%endblock content%}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the <code>task_form.html<\/code>, we render the form fields manually. If you want to automatically generate the form, you can use one of the following attributes:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">{{ form.as_p }}   <span class=\"hljs-comment\"># render the form as &lt;p&gt;<\/span>\n{{ form.as_div }} <span class=\"hljs-comment\"># render the form as &lt;div&gt;<\/span>\n{{ form.as_ul }}  <span class=\"hljs-comment\"># redner the form as &lt;ul&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='defining-the-route'>Defining the route <a href=\"#defining-the-route\" class=\"anchor\" id=\"defining-the-route\" title=\"Anchor for Defining the route\">#<\/a><\/h2>\n\n\n\n<p>Add a route to the <code>urls.py<\/code> of the todo application by mapping an URL with the result of the <code>as_view()<\/code> method of the <code>TaskCreate<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> django.urls <span class=\"hljs-keyword\">import<\/span> path\n<span class=\"hljs-keyword\">from<\/span> .views <span class=\"hljs-keyword\">import<\/span> home, TaskList, TaskDetail, TaskCreate\n\nurlpatterns = &#91;\n    path(<span class=\"hljs-string\">''<\/span>, home, name=<span class=\"hljs-string\">'home'<\/span>),\n    path(<span class=\"hljs-string\">'tasks\/'<\/span>, TaskList.as_view(),name=<span class=\"hljs-string\">'tasks'<\/span>),\n    path(<span class=\"hljs-string\">'task\/&lt;int:pk&gt;\/'<\/span>, TaskDetail.as_view(),name=<span class=\"hljs-string\">'task'<\/span>),\n    path(<span class=\"hljs-string\">'task\/create\/'<\/span>, TaskCreate.as_view(),name=<span class=\"hljs-string\">'task-create'<\/span>),\n]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='displaying-flash-messages-adding-a-link-to-the-navigation'>Displaying flash messages &amp; adding a link to the navigation <a href=\"#displaying-flash-messages-adding-a-link-to-the-navigation\" class=\"anchor\" id=\"displaying-flash-messages-adding-a-link-to-the-navigation\" title=\"Anchor for Displaying flash messages &amp; adding a link to the navigation\">#<\/a><\/h2>\n\n\n\n<p>Modify the <code>base.html<\/code> template of the project to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Display the flash messages.<\/li>\n\n\n\n<li>Add the <code>New Task<\/code> link to the navigation.<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-code-table\"><span class='shcb-loc'><span>{%load static %}\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-meta\">&lt;!DOCTYPE <span class=\"hljs-meta-keyword\">html<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">html<\/span> <span class=\"hljs-attr\">lang<\/span>=<span class=\"hljs-string\">\"en\"<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span>    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">head<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">meta<\/span> <span class=\"hljs-attr\">charset<\/span>=<span class=\"hljs-string\">\"UTF-8\"<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">meta<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"viewport\"<\/span> <span class=\"hljs-attr\">content<\/span>=<span class=\"hljs-string\">\"width=device-width, initial-scale=1.0\"<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">link<\/span> <span class=\"hljs-attr\">rel<\/span>=<span class=\"hljs-string\">\"stylesheet\"<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"{% static 'css\/style.css' %}\"<\/span> \/&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">title<\/span>&gt;<\/span>Todo List<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">title<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">head<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span>    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">body<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">header<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"header\"<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"container\"<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>            \t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"{%url 'home'%}\"<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"logo\"<\/span>&gt;<\/span>Todo<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">nav<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"nav\"<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>                \t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"{% url 'home'%}\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">i<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bi bi-house-fill\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">i<\/span>&gt;<\/span> Home<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>                \t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"{% url 'tasks' %}\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">i<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bi bi-list-task\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">i<\/span>&gt;<\/span> My Tasks<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span>\n<\/span><\/span><mark class='shcb-loc'><span>                \t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"{% url 'task-create' %}\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">i<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bi bi-plus-circle\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">i<\/span>&gt;<\/span> Create Task<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span>\n<\/span><\/mark><span class='shcb-loc'><span>                <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">nav<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">header<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">main<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"container\"<\/span>&gt;<\/span>\n<\/span><\/span><mark class='shcb-loc'><span>            \t{% if messages %}\n<\/span><\/mark><mark class='shcb-loc'><span>\t\t\t\t{% for message in messages %}\n<\/span><\/mark><mark class='shcb-loc'><span>\t\t\t\t\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"alert alert-{{message.tags}}\"<\/span>&gt;<\/span>\n<\/span><\/mark><mark class='shcb-loc'><span>\t\t\t\t\t       {{message}}\n<\/span><\/mark><mark class='shcb-loc'><span>\t\t\t\t\t     <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<\/span><\/mark><mark class='shcb-loc'><span>\t\t\t\t\t   {% endfor %}\n<\/span><\/mark><mark class='shcb-loc'><span>\t\t\t\t{% endif %}\n<\/span><\/mark><span class='shcb-loc'><span>            \n<\/span><\/span><span class='shcb-loc'><span>             {%block content %}\n<\/span><\/span><span class='shcb-loc'><span>             {%endblock content%}\n<\/span><\/span><span class='shcb-loc'><span>            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">main<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">footer<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"footer\"<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"container\"<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>\t\t\t\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>\u00a9 Copyright {% now \"Y\" %} by <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"https:\/\/www.pythontutorial.net\"<\/span>&gt;<\/span>Python Tutorial<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">footer<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">body<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">html<\/span>&gt;<\/span>\n<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Run the Django dev server and open the URL <code>http:\/\/127.0.0.1:8000\/task\/create\/<\/code>, you&#8217;ll see the following form:<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"610\" style=\"aspect-ratio: 712 \/ 610;\" width=\"712\" controls src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/12\/Django-CreateView-Demo.mp4\"><\/video><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"651\" height=\"646\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/django-todo-list-CreateView.png\" alt=\"\" class=\"wp-image-5983\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/django-todo-list-CreateView.png 651w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/django-todo-list-CreateView-300x298.png 300w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/django-todo-list-CreateView-150x150.png 150w\" sizes=\"auto, (max-width: 651px) 100vw, 651px\" \/><\/figure>\n\n\n\n<p>Enter the title and description and click the Save button, you&#8217;ll be redirected to the task list page with a message:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"711\" height=\"531\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/django-todo-list-CreateView-example.png\" alt=\"\" class=\"wp-image-5984\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/django-todo-list-CreateView-example.png 711w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/11\/django-todo-list-CreateView-example-300x224.png 300w\" sizes=\"auto, (max-width: 711px) 100vw, 711px\" \/><\/figure>\n\n\n\n<p class=\"note\">You can download the final code for this <a href=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/12\/todo_list_4_create_view.zip\">Django CreateView tutorial<\/a> here.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the Django <code>CreateView<\/code> class to define a class-based view that creates an object.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"5980\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-createview\/\"\n\t\t\t\tdata-post-title=\"Django CreateView\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"5980\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-createview\/\"\n\t\t\t\tdata-post-title=\"Django CreateView\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the Django CreateView class to define a class-based view that creates a task for the Todo application.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":5531,"menu_order":15,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5980","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5980","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=5980"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5980\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5531"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=5980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}