UpdateView की मूल बातें
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.
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.
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.
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. #}
- URL के लिए एक
pk(या slug) ज़रूरी है यह भूल जाना, ताकि UpdateView को पता चले कि किस existing object को edit करना है। fieldsको CreateView से अलग list में set करना और confused होना जब कुछ fields silently show न हों।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 शुरू होता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: