CreateView की मूल बातें
from django.views.generic.edit import CreateView
class ModelNameCreateView(CreateView):
model = ModelName
fields = ['field1', 'field2']
success_url = '/success-url/'
एक Minimal CreateView
एक CreateView subclass पर model और fields set करना Django के लिए अपने आप एक ModelForm बनाने और submit और valid होने पर एक नई row save करने के लिए काफी है।
उदाहरण: A Minimal CreateView
BookCreateView auto-generates a form for title, author, and published_date, then redirects to book-list on success.
from django.views.generic.edit import CreateView
from django.urls import reverse_lazy
from .models import Book
class BookCreateView(CreateView):
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. #}
Create Form Render करना
CreateView template context में एक form object pass करता है, जो किसी भी दूसरे Django form जैसे render होता है, required CSRF token सहित।
<form> action को empty छोड़ें या इसे खुद की तरफ point करें।उदाहरण: Rendering the Create Form
The form variable is the auto-built ModelForm; {% csrf_token %} is still required for the POST to succeed.
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save Book</button>
</form>
{# 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. #}
एक Successful Save के बाद Redirect करना
success_url CreateView को बताता है कि नया object save होने के बाद browser को कहां भेजना है। reverse_lazy reverse के बजाय उपयोग किया जाता है क्योंकि URL class define होने पर evaluate होता है, URLconf पूरी तरह load होने से पहले।
उदाहरण: Redirecting After a Successful Save
reverse_lazy("book-list") resolves the book-list URL only when it is actually needed, avoiding import-time errors.
from django.urls import reverse_lazy
from django.views.generic.edit import CreateView
from .models import Book
class BookCreateView(CreateView):
model = Book
fields = ['title', 'author']
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. #}
- एक CreateView पर
fields(याexclude) set करना भूल जाना, जो एक error raise करता है क्योंकि Django को नहीं पता कौन से fields expose करने हैं। success_urlया model परget_absolute_urlset न करना, इसलिए Django एक successful save के बाद redirect नहीं कर सकता।- यह मान लेना कि CreateView अपने आप login require करता है — यह नहीं करता, जब तक
LoginRequiredMixinके साथ combine न किया जाए।
- CreateView एक model से एक form generate करता है और उस form valid होने पर एक नया object save करता है।
fields(याexclude) CreateView को बताता है कि कौन से model fields form inputs के रूप में expose करने हैं।success_urlcontrol करता है कि एक successful creation के बाद browser कहां redirect करे।- CreateView को अब भी इसके template में
{% csrf_token %}चाहिए बिल्कुल एक manual form जैसा।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: