{"id":5986,"date":"2022-12-01T04:35:07","date_gmt":"2022-12-01T04:35:07","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=5986"},"modified":"2023-08-17T09:46:57","modified_gmt":"2023-08-17T09:46:57","slug":"django-updateview","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/django-tutorial\/django-updateview\/","title":{"rendered":"Django UpdateView"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Django <code>UpdateView<\/code> class to create a class-based view that edits an existing object.<\/p>\n\n\n\n<p class=\"note\">This tutorial begins where&nbsp;the&nbsp;<a href=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-createview\/\">Django CreateView tutorial<\/a>&nbsp;left off.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='defining-the-updateview-class'>Defining the UpdateView class <a href=\"#defining-the-updateview-class\" class=\"anchor\" id=\"defining-the-updateview-class\" title=\"Anchor for Defining the UpdateView class\">#<\/a><\/h2>\n\n\n\n<p>The <code>UpdateView<\/code> class allows you to create a class-based view that:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Display a form for editing an existing object.<\/li>\n\n\n\n<li>Redisplay the form if it has validation errors.<\/li>\n\n\n\n<li>Save changes of the object to the database.<\/li>\n<\/ul>\n\n\n\n<p>The form is generated automatically from the <a href=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-models\/\">object&#8217;s model<\/a> unless you explicitly specify a <a href=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-form\/\">form class<\/a>.<\/p>\n\n\n\n<p>To demonstrate the Django <code>UpdateView<\/code> class, we&#8217;ll create a view that edits a task of the <a href=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-todo-app\/\">Todo App<\/a>.<\/p>\n\n\n\n<p>To do that we modify the <code>views.py<\/code> of the todo app and define the <code>TaskUpdate<\/code> class that inherits from the <code>UpdateView<\/code> class like this:<\/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, UpdateView\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\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">TaskUpdate<\/span><span class=\"hljs-params\">(UpdateView)<\/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    <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        messages.success(self.request, <span class=\"hljs-string\">\"The task was updated successfully.\"<\/span>)\n        <span class=\"hljs-keyword\">return<\/span> super(TaskUpdate,self).form_valid(form)\n\n<span class=\"hljs-comment\"># ...<\/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>UpdateView<\/code> from the <code>django.views.generic.edit<\/code>:<\/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\"><span class=\"hljs-keyword\">from<\/span> django.views.generic.edit <span class=\"hljs-keyword\">import<\/span> CreateView, UpdateView<\/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>Second, define the <code><code>TaskUpdate<\/code><\/code> class that inherits from the <code>UpdateView<\/code> class. In the <code><code>TaskUpdate<\/code><\/code> class 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 edited. Because we specify the Task as the model in this example.<\/li>\n\n\n\n<li><code>fields<\/code> is a list that specifies the form fields. In this example, we use title, description, and completed fields.<\/li>\n\n\n\n<li><code><code>success_url<\/code><\/code> is the target URL (task list) that Django will redirect to once a task is updated successfully.<\/li>\n\n\n\n<li><code>form_valid()<\/code> &#8211; the method is called once the form is posted successfully. In this example, we create a flash message 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>TaskUpdate<\/code> class uses the <code>task_form.html<\/code> template from the <code>templates\/todo<\/code> directory. Note that the <code><a href=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-createview\/\">CreateView<\/a><\/code> and <code>UpdateView<\/code> classes share the same template name.<\/p>\n\n\n\n<p>If you want to use a different template name, you can specify it using the <code>template_name<\/code> attribute.<\/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>Modify the <code>task_form.html<\/code> template that shows the <code>Update Task<\/code> heading if the task variable is available in the template (editing mode) or <code>Create Task<\/code> otherwise (creating mode).<\/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 shcb-code-table\"><span class='shcb-loc'><span>{%extends 'base.html'%}\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span>{%block content%}\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\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"center\"<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span> <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<\/span><\/span><span class='shcb-loc'><span> \t{%csrf_token %}\n<\/span><\/span><mark class='shcb-loc'><span> \t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h2<\/span>&gt;<\/span>{% if task %} Update {%else %} Create {%endif%} Task<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h2<\/span>&gt;<\/span>\n<\/span><\/mark><span class='shcb-loc'><span>\t{% for field in form %}\n<\/span><\/span><span class='shcb-loc'><span>\t\t{% if field.name == 'completed' %}\n<\/span><\/span><span class='shcb-loc'><span>\t\t\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>\t\t\t\t{{ field.label_tag }}\n<\/span><\/span><span class='shcb-loc'><span>\t\t\t\t{{ field }}\n<\/span><\/span><span class='shcb-loc'><span>\t\t\t<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>\t\t\t{% if field.errors %}\n<\/span><\/span><span class='shcb-loc'><span>        \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<\/span><\/span><span class='shcb-loc'><span>        \t{% endif %}\n<\/span><\/span><span class='shcb-loc'><span>\t\t{% else %}\n<\/span><\/span><span class='shcb-loc'><span>    \t\t{{ field.label_tag }} \n<\/span><\/span><span class='shcb-loc'><span>        \t{{ field }}\n<\/span><\/span><span class='shcb-loc'><span>        \t{% if field.errors %}\n<\/span><\/span><span class='shcb-loc'><span>        \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<\/span><\/span><span class='shcb-loc'><span>        \t{% endif %}\n<\/span><\/span><span class='shcb-loc'><span>        {% endif %}\n<\/span><\/span><span class='shcb-loc'><span>\t{% endfor %}\n<\/span><\/span><span class='shcb-loc'><span>\t\n<\/span><\/span><span class='shcb-loc'><span>\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<\/span><\/span><span class='shcb-loc'><span>\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<\/span><\/span><span class='shcb-loc'><span>\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<\/span><\/span><span class='shcb-loc'><span>\t<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>\t\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\">form<\/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>\n<\/span><\/span><span class='shcb-loc'><span>{%endblock content%}\n<\/span><\/span><\/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<h2 class=\"wp-block-heading\" id='defining-a-route'>Defining a route <a href=\"#defining-a-route\" class=\"anchor\" id=\"defining-a-route\" title=\"Anchor for Defining a route\">#<\/a><\/h2>\n\n\n\n<p>Define a route in the <code>urls.py<\/code> of the todo app that maps a URL with the result of the <code>as_view()<\/code> method of the <code>TaskUpdate<\/code> class:<\/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 shcb-code-table\"><span class='shcb-loc'><span><span class=\"hljs-keyword\">from<\/span> django.urls <span class=\"hljs-keyword\">import<\/span> path\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-keyword\">from<\/span> .views <span class=\"hljs-keyword\">import<\/span> (\n<\/span><\/span><span class='shcb-loc'><span>    home, \n<\/span><\/span><span class='shcb-loc'><span>    TaskList, \n<\/span><\/span><span class='shcb-loc'><span>    TaskDetail, \n<\/span><\/span><span class='shcb-loc'><span>    TaskCreate, \n<\/span><\/span><span class='shcb-loc'><span>    TaskUpdate\n<\/span><\/span><span class='shcb-loc'><span>)\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span>urlpatterns = &#91;\n<\/span><\/span><span class='shcb-loc'><span>    path(<span class=\"hljs-string\">''<\/span>, home, name=<span class=\"hljs-string\">'home'<\/span>),\n<\/span><\/span><span class='shcb-loc'><span>    path(<span class=\"hljs-string\">'tasks\/'<\/span>, TaskList.as_view(),name=<span class=\"hljs-string\">'tasks'<\/span>),\n<\/span><\/span><span class='shcb-loc'><span>    path(<span class=\"hljs-string\">'task\/&lt;int:pk&gt;\/'<\/span>, TaskDetail.as_view(),name=<span class=\"hljs-string\">'task'<\/span>),\n<\/span><\/span><span class='shcb-loc'><span>    path(<span class=\"hljs-string\">'task\/create\/'<\/span>, TaskCreate.as_view(),name=<span class=\"hljs-string\">'task-create'<\/span>),\n<\/span><\/span><mark class='shcb-loc'><span>    path(<span class=\"hljs-string\">'task\/update\/&lt;int:pk&gt;\/'<\/span>, TaskUpdate.as_view(),name=<span class=\"hljs-string\">'task-update'<\/span>),\n<\/span><\/mark><span class='shcb-loc'><span>]\n<\/span><\/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='including-an-edit-link'>Including an edit link <a href=\"#including-an-edit-link\" class=\"anchor\" id=\"including-an-edit-link\" title=\"Anchor for Including an edit link\">#<\/a><\/h2>\n\n\n\n<p>Modify the <code>task_list.html<\/code> template to include the edit link for each task on the task list:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" 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>{%extends 'base.html'%}\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span>{%block content%}\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\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"center\"<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h2<\/span>&gt;<\/span>My Todo List<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h2<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>\t{% if tasks %}\n<\/span><\/span><span class='shcb-loc'><span>\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">ul<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"tasks\"<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>\t\t{% for task in tasks %}\n<\/span><\/span><span class='shcb-loc'><span>\t\t\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"{% url 'task' task.id %}\"<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"{% if task.completed%}completed{%endif%}\"<\/span>&gt;<\/span>{{ task.title }}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span> \n<\/span><\/span><span class='shcb-loc'><span>\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\">\"task-controls\"<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>\t\t\t\t \t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"#\"<\/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-trash\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">i<\/span>&gt;<\/span> <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span>\n<\/span><\/span><mark class='shcb-loc'><span>\t\t\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 'task-update' task.id %}\"<\/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-pencil-square\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">i<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span>\n<\/span><\/mark><span class='shcb-loc'><span>\t\t         <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>\t\t    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n<\/span><\/span><span class='shcb-loc'><span>\t\t{% endfor %}\n<\/span><\/span><span class='shcb-loc'><span>\t{% else %}\n<\/span><\/span><span class='shcb-loc'><span>\t\t<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>? Yay, you have no pending tasks! <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>Create Task<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>\t{% endif %}\n<\/span><\/span><span class='shcb-loc'><span>\t<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">ul<\/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>{%endblock content%}\n<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><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>If you edit a task from the todo list by appending three asterisks (<code>***<\/code>) to the title and mark the task as completed:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"517\" height=\"522\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/12\/django-todo-list-UpdateView.png\" alt=\"\" class=\"wp-image-5988\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/12\/django-todo-list-UpdateView.png 517w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/12\/django-todo-list-UpdateView-297x300.png 297w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/12\/django-todo-list-UpdateView-150x150.png 150w\" sizes=\"auto, (max-width: 517px) 100vw, 517px\" \/><\/figure>\n\n\n\n<p>Click the Save button and you&#8217;ll see that the title and status of the task are updated:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"582\" height=\"257\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/12\/django-todo-list-UpdateView-example.png\" alt=\"\" class=\"wp-image-5991\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/12\/django-todo-list-UpdateView-example.png 582w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/12\/django-todo-list-UpdateView-example-300x132.png 300w\" sizes=\"auto, (max-width: 582px) 100vw, 582px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"596\" style=\"aspect-ratio: 718 \/ 596;\" width=\"718\" controls src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/12\/Django-UpdateView-Demo.mp4\"><\/video><\/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_5_update_view.zip\">Django UpdateView 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>Define a new class that inherits from the <code>UpdateView<\/code> class to create a class-based view that edits an existing 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=\"5986\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-updateview\/\"\n\t\t\t\tdata-post-title=\"Django UpdateView\"\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=\"5986\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-updateview\/\"\n\t\t\t\tdata-post-title=\"Django UpdateView\"\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 UpdateView class to create a class-based view that edits an existing object.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":5531,"menu_order":16,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5986","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5986","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=5986"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5986\/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=5986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}