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

CreateView की मूल बातें

CreateView एक ready-made form page है जो आपको save logic खुद लिखे बिना database में एक बिल्कुल नई row add करने देता है।
Syntax
markup
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.

markup
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 सहित।

Warning: एक CreateView template को वापस उसी URL पर POST करना चाहिए, इसलिए <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.

markup
<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.

markup
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. #}
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. एक CreateView पर fields (या exclude) set करना भूल जाना, जो एक error raise करता है क्योंकि Django को नहीं पता कौन से fields expose करने हैं।
  2. success_url या model पर get_absolute_url set न करना, इसलिए Django एक successful save के बाद redirect नहीं कर सकता।
  3. यह मान लेना कि CreateView अपने आप login require करता है — यह नहीं करता, जब तक LoginRequiredMixin के साथ combine न किया जाए।
चैप्टर सारांश
  • CreateView एक model से एक form generate करता है और उस form valid होने पर एक नया object save करता है।
  • fields (या exclude) CreateView को बताता है कि कौन से model fields form inputs के रूप में expose करने हैं।
  • success_url control करता है कि एक successful creation के बाद browser कहां redirect करे।
  • CreateView को अब भी इसके template में {% csrf_token %} चाहिए बिल्कुल एक manual form जैसा।

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.