Open In App

UpdateView - Class Based Views Django

Last Updated : 19 Nov, 2025
Comments
Improve
Suggest changes
11 Likes
Like
Report

An UpdateView is a built-in class-based view used to edit an existing record in the database. It automatically handles fetching the record, showing a pre-filled form, validating input, and saving changes.

  • Specify the model that contains the record to be updated.
  • Define the form fields that should appear in the update form.
  • Provide the template used to render the form.
  • Set a success URL to redirect after the update is saved.

Example: Consider a project named 'geeksforgeeks' having an app named 'geeks'. After you have a project and an app, let's create a model of which we will be creating instances through our view.

In geeks/models.py:

Python
from django.db import models
 
# declare a new model with a name "GeeksModel"
class GeeksModel(models.Model):

    # fields of the model
    title = models.CharField(max_length = 200)
    description = models.TextField()

    # renames the instances of the model with their title name
    def __str__(self):
        return self.title

After creating this model, we need to run two commands in order to create Database for the same.

Python manage.py makemigrations
Python manage.py migrate

Now, create instances of this model using the Django shell by running the following command in the terminal:

Python manage.py shell

Next enter following commands:

>>> from geeks.models import GeeksModel
>>> GeeksModel.objects.create( title="title1", description="description1")
>>> GeeksModel.objects.create(title="title2", description="description2")
>>> GeeksModel.objects.create(title="title3", description="description3")

Now that the backend setup complete, verify that instances have been created by visiting:

django-listview-check-models-instances

To create an UpdateView, it is only necessary to specify the model. Django’s UpdateView will then look for a template named app_name/modelname_form.html. Here, expected template path is geeks/templates/geeks/geeksmodel_form.html.

Next, create the class-based view in geeks/views.py:

Python
from django.views.generic.edit import UpdateView

# Relative import of GeeksModel
from .models import GeeksModel

class GeeksUpdateView(UpdateView):
    # specify the model you want to use
    model = GeeksModel

    # specify the fields
    fields = [
        "title",
        "description"
    ]
    success_url ="/"

Now, create a url path to map the view in geeks/urls.py:

Python
from django.urls import path 
  
# importing views from views.py 
from .views import GeeksUpdateView 
urlpatterns = [ 
    # <pk> is identification for id field, <slug> can also be used 
    path('<pk>/update', GeeksUpdateView.as_view()), 
] 

Next, create a template in templates/geeks/geeksmodel_form.html:

html
<form method="post"> 
    {% csrf_token %} 
    {{ form.as_p }} 
    <input type="submit" value="Save"> 
</form> 

Now, visit the corresponding page to verify that the UpdateView is working as expected.

django-updateview-class-based-view
Suggested Quiz
3 Questions

What is the main purpose of the Django UpdateView class?

  • A

    To display a list of objects from the database

  • B

    To create a new object in the database

  • C

    To edit an existing object in the database

  • D

    To delete an object from the database

Explanation:


Which attributes are typically required when defining an UpdateView subclass to work properly?

  • A

    model, fields or form_class, success_url

  • B

    template_name only

  • C

    DATABASES and INSTALLED_APPS

  • D

    STATIC_URL and MEDIA_ROOT

Explanation:

UpdateView needs to know which model to update, which fields to display in the form (or a custom form class), and where to redirect after a successful update.

When a web request for updating an object arrives via GET method, what does UpdateView do by default?

  • A

    Immediately update the object in the database

  • B

    Show a form populated with the object’s current data

  • C

    Show an empty form for a new object

  • D

    Redirect to success_url

Explanation:

On GET request UpdateView retrieves the object and returns a form pre-filled with existing data so user can modify it.

Quiz Completed Successfully
Your Score :   2/3
Accuracy :  0%
Login to View Explanation
1/3 1/3 < Previous Next >

Explore