← Back to Django Course | Chapter 9: Class-Based Views | Lesson 6 of 10

UpdateView की मूल बातें

UpdateView एक ready-made form page है जो आपको database में एक existing row edit करने देता है, जो पहले से इसकी current values के साथ pre-filled होता है।
Syntax
markup
from django.views.generic.edit import UpdateView

class ModelNameUpdateView(UpdateView):
    model = ModelName
    fields = ['field1', 'field2']
    success_url = '/success-url/'

# urls.py
path('route/<int:pk>/edit/', ModelNameUpdateView.as_view())

एक Minimal UpdateView

UpdateView को model, fields, और pk वाला एक URL pattern चाहिए ताकि यह existing object load कर सके, form pre-fill कर सके, और changes उसी row में वापस save कर सके।

उदाहरण: A Minimal UpdateView

BookUpdateView loads the Book matching the URL's pk and pre-fills the form with its current field values.

markup
from django.views.generic.edit import UpdateView
from django.urls import reverse_lazy
from .models import Book


class BookUpdateView(UpdateView):
    model = Book
    fields = ['title', 'author', 'published_date']
    success_url = reverse_lazy('book-list')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Edit URL Wire करना

URL pattern को pk capture करना चाहिए ताकि Django को exactly पता हो कि pre-filled edit form render करने से पहले कौन सा object load करना है।

उदाहरण: Wiring the Edit URL

The <int:pk> segment lets UpdateView run Book.objects.get(pk=pk) before showing the form.

markup
from django.urls import path
from .views import BookUpdateView

urlpatterns = [
    path('books/<int:pk>/edit/', BookUpdateView.as_view(), name='book-edit'),
]
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

get_success_url के साथ एक Dynamic Redirect

एक static success_url set करने के बजाय get_success_url() को override करने से redirect उस specific object पर depend कर सकता है जो अभी update हुआ है, जैसे user को उस object के अपने detail page पर भेजना।

उदाहरण: A Dynamic Redirect with get_success_url

self.object is the just-saved Book instance, so the redirect goes straight to that book's detail page.

markup
from django.urls import reverse
from django.views.generic.edit import UpdateView
from .models import Book


class BookUpdateView(UpdateView):
    model = Book
    fields = ['title', 'author']

    def get_success_url(self):
        return reverse('book-detail', kwargs={'pk': self.object.pk})
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. URL के लिए एक pk (या slug) ज़रूरी है यह भूल जाना, ताकि UpdateView को पता चले कि किस existing object को edit करना है।
  2. fields को CreateView से अलग list में set करना और confused होना जब कुछ fields silently show न हों।
  3. get_success_url() को override न करना जब redirect target उस specific object पर depend करे जो अभी edit हुआ है।
चैप्टर सारांश
  • UpdateView एक existing object load करता है (URL से pk या slug के through) और इसकी current values के साथ एक ModelForm pre-fill करता है।
  • fields control करता है कि कौन से model fields editable हैं, बिल्कुल CreateView जैसा।
  • get_success_url() को save होने के बाद एक dynamic, per-object redirect के लिए override किया जा सकता है।
  • Template एक CreateView template जैसा दिखता और behave करता है, लेकिन form pre-populated शुरू होता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.