How to Create and Use UpdateView in Django?

Published On: 20/09/2022 | Category: Django


Hi Dev,

This tutorial will give you example of how to use updateview in django. This article goes in detailed on django updateview example. let’s discuss about how to create and use updateview in django with example. I explained simply step by step django updateview class based views example. you will do the following things for django generic updateview example.

Django Class based views are simpler and efficient to manage than function-based views. A function based view with tons of lines of code can be converted into a class based views comes with few lines only.

So, in this example, UpdateView refers to a view (logic) to update a particular instance of a table from the database with some extra details. It is used to update entries in the database

let's see bellow example here you will learn how to use updateview in django.

Step 1: Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example

Step 2: Create a App

Now we'll create a single app called core to store a list of post names. We're keeping things intentionally basic. Stop the local server with Control+c and use the startapp command to create this new app.

python3 manage.py startapp core

Step 3: Update setting.py

Next, then update INSTALLED_APPS within our settings.py file to notify Django about the app.

Next, you need to add it in the settings.py file as follows:

settings.py

....
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'core',
]

Step 4: Creating the Model

In this step we will require the database model for storing article details in article table.Open the models.py file and add the following code:

core/models.py
from django.db import models

class Article(models.Model):
    title= models.CharField(max_length=300)
    content= models.TextField()
    pub_date = models.DateTimeField(auto_now_add= True)

Ok, all set. We can engender a migrations file for this change, then integrate it to our database via migrate.

python manage.py makemigrations
python manage.py migrate

Step 5: Creating the Views

In this step, we need to configure our views. The ArticleUpdateView page will template and define and Article model as well as, open the views.py file and add:

core/models.py
from django.shortcuts import render
from django.views.generic.edit import UpdateView
from .models import *

class ArticleUpdateView(UpdateView):
    model = Article

    form_class = ArticleForm

    success_url ="/"

Step 6: Creating the Template

Next, then with your text editor create new templates files: core/templates/article_form.html file and the add:

core/templates/article_form.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Tuts-Station.com</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
    <style type="text/css">
        body{
            background-color: #f7fcff;
        }
    </style>
</head>
<body>
    <div class="container mt-5 pt-5">
        <div class="row d-flex justify-content-center">
            <div class="col-md-10">
                <div class="card">
                    <div class="card-header">
                        <h4>How to Create and Use UpdateView in Django? - <span class="text-primary">Tuts-Station.com</span></h4>
                    </div>
                    <div class="card-body">
                        <form method = "post" enctype="multipart/form-data">
                            {% csrf_token %}
                            {{ form.as_p }}
                            <button type="submit" class="btn btn-success">Submit</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

Step 7: Creating URLs

In this section, we need a urls.py file within the core app however Django doesn't create one for us with the startapp command. Create core/urls.py with your text editor and within this file we'll import yet-to-be-created function for each--ArticleUpdateView Note as well that we set an optional URL name for each.

Here's what it looks like:

core/urls.py
from django.urls import path
from .views import ArticleUpdateView

urlpatterns = [
    path('update/<pk>', ArticleUpdateView.as_view()),
]

Next, we require to add a URL path for our example app which can be done by importing include and setting a path for it.

example/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('core.urls')),
]

Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python manage.py runserver

Next, go to the http://localhost:8000/update/{id} address with a web browser.

I Hope It will help you....

Happy Pythonic Coding!