Introduction
UpdateView helps you easily create a page to edit existing data in your database without writing much code.
Jump into concepts and practice - no test required
UpdateView helps you easily create a page to edit existing data in your database without writing much code.
from django.views.generic.edit import UpdateView from .models import YourModel class YourModelUpdateView(UpdateView): model = YourModel fields = ['field1', 'field2'] template_name = 'yourmodel_form.html' success_url = '/success/'
model tells Django which data to edit.
fields lists the fields users can change.
class BookUpdateView(UpdateView): model = Book fields = ['title', 'author'] template_name = 'book_edit.html' success_url = '/books/'
class ProfileUpdateView(UpdateView): model = Profile fields = ['bio', 'avatar'] success_url = '/profile/'
This code creates a page to edit an Article's title and content. When saved, it redirects to /articles/.
from django.urls import path from django.views.generic.edit import UpdateView from django.db import models # Simple model class Article(models.Model): title = models.CharField(max_length=100) content = models.TextField() # UpdateView for editing Article class ArticleUpdateView(UpdateView): model = Article fields = ['title', 'content'] template_name = 'article_form.html' success_url = '/articles/' # URL pattern urlpatterns = [ path('article/<int:pk>/edit/', ArticleUpdateView.as_view(), name='article_edit'), ] # Template (article_form.html) example: # <form method="post"> # {% csrf_token %} # {{ form.as_p }} # <button type="submit">Save</button> # </form>
Make sure your template includes the CSRF token for security.
The URL must include the primary key (pk) to tell Django which item to edit.
You can customize the success_url to control where users go after saving.
UpdateView makes editing existing data easy with little code.
Set model, fields, template_name, and success_url to configure it.
Use URL patterns with pk to link to the right item for editing.
UpdateView?UpdateView is designed to edit existing data, not create or delete.CreateView is for new records, DeleteView for deleting, and list views for showing data.UpdateView?fields.field_names, form_fields, and update_fields are not valid attributes for UpdateView.UpdateView snippet, what will happen after a successful form submission?class ArticleUpdate(UpdateView):
model = Article
fields = ['title', 'body']
template_name = 'article_edit.html'
success_url = '/articles/'success_url attribute defines where to go after a successful update.success_url = '/articles/' means redirect to that URL after saving.UpdateView code:class BookUpdate(UpdateView):
model = Book
fields = ['name', 'author']
template_name = 'book_edit.html'
urlpatterns = [
path('book/edit/', BookUpdate.as_view(), name='book_edit'),
]'book/edit/' has no pk or id parameter, so the view won't know which book to update.UpdateView for a Profile model that only allows editing the bio and location fields. You also want to redirect users to their profile detail page after saving. Which is the best way to implement this?fields must be exactly ['bio', 'location']. C includes extra 'email'. D uses form_class which doesn't limit fields here.pk. Fixed success_url in B and D won't work for specific profile. The correct implementation uses get_success_url with reverse and self.object.pk.