UpdateView Django में एक व्यू है जिसका उपयोग फ्रंटएंड से किसी भी मॉडल डेटा को अपडेट करने के लिए किया जाता है। यह एक बिल्ट-इन व्यू है जिसे आसानी से लागू किया जा सकता है। यह व्यू को अपडेट करने में एडमिन पेज की तरह काम करता है। इस लेख में, हम एक उदाहरण लेंगे और प्रदर्शित करेंगे कि Django में UpdateView का उपयोग कैसे करें।
सबसे पहले, एक Django प्रोजेक्ट और एक ऐप बनाएं। मैंने प्रोजेक्ट को "tutorial11" नाम से और ऐप को "modelFormsDemo" नाम से बनाया।
अब, कुछ बुनियादी चीजें करते हैं।
settings.py में ऐप जोड़ें:
INSTALLED_APPS += ['modelFormsDemo']
प्रोजेक्ट के urls.py में, ऐप के urls शामिल करें।
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('modelFormsDemo.urls'))
]
ऐप के urls.py में, निम्नलिखित जोड़ें:
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.home, name="home"),
path('student/edit//', views.StudentUpdateView.as_view(), name="update"),
path('success/', views.success, name='success')
]
यहां हमने तीन urls बनाए: एक फ्रंटएंड रेंडर करने के लिए, एक अपडेट के लिए UpdateView, और अपडेट के बाद रीडायरेक्ट के लिए success।
उदाहरण
models.py में, यह जोड़ें:
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
standard = models.CharField(max_length=100)
section = models.CharField(max_length=100)
यहां हमने एक साधारण मॉडल बनाया। यह एक बहुत ही बेसिक मॉडल है।
views.py में, निम्नलिखित जोड़ें:
from django.shortcuts import render
from .forms import StudentForm
from django.views.generic.edit import UpdateView
from .models import Student
from django.urls import reverse_lazy
def home(request):
if request.method == 'POST':
form = StudentForm(request.POST)
if form.is_valid():
form.save()
stuForm = StudentForm()
return render(request, 'home.html', {"stu_form": stuForm})
class StudentUpdateView(UpdateView):
model = Student
fields = "__all__"
template_name = 'update_view.html'
success_url = '/success/'
def success(request):
return render(request, 'success.html')
यहां हमने कुछ भी जटिल नहीं किया; हमने बस मॉडल को नाम दिया, फील्ड्स, और वह टेम्पलेट जिसे हम रेंडर करने जा रहे हैं। साथ ही, हमने एक फंक्शन परिभाषित किया जो बताता है कि अपडेट के बाद क्या करना है।
ऐप डायरेक्टरी में forms.py बनाएं और निम्नलिखित लाइनें जोड़ें:
from django import forms
from .models import Student
class StudentForm(forms.ModelForm):
class Meta:
model = Student
fields = ['name', 'standard', 'section']
यहां हमने बस एक फॉर्म बनाया जिसे हम रेंडर करेंगे।
अब एक templates फोल्डर बनाएं और इसमें तीन फाइलें जोड़ें: home.html, update_view.html, और success.html।
home.html और update_view.html में:
<!DOCTYPE html>
<html>
<head>
<title>TUT</title>
</head>
<body>
{% for fm in stu_form %}
<form method="post">
{% csrf_token %}
{{fm.errors}}<br>
{{fm.label}}:{{fm}}<br>
{% endfor %}
<button type="submit">Submit</button>
</form>
</body>
</html>
success.html में, निम्नलिखित लाइनें जोड़ें:
<!DOCTYPE html>
<html>
<head>
<title>TUT</title>
</head>
<body>
<h2>Success</h2>
</body>
</html>
इसके साथ, सब कुछ सेट है। अब आप आउटपुट जांचने के लिए आगे बढ़ सकते हैं।
आउटपुट
Home.html -

अब यदि आप https://127.0.0.1:8000/student/edit/(student object id)/ पर जाते हैं तो आप हमारा update_view.html देखेंगे।
Update_view.html -
