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

Generic View के Mixins

एक mixin एक छोटी add-on class है जो एक class-based view को extra powers देती है, जैसे एक plain car को पूरी car फिर से बनाए बिना एक GPS देना।
Syntax
markup
from django.contrib.auth.mixins import LoginRequiredMixin

class ViewName(LoginRequiredMixin, ListView):
    model = ModelName

एक Mixin एक View में क्या जोड़ता है

एक mixin बस एक Python class है जो multiple inheritance के through दूसरी class के साथ combine होने के लिए बनी है। Django के generic-view mixins checks जोड़ते हैं (जैसे "क्या यह user logged in है?") जो view की normal get()/post() logic execute होने से पहले run होते हैं।

उदाहरण: What a Mixin Adds to a View

LoginRequiredMixin runs its check before ListView's own logic, blocking anonymous visitors from ever reaching the list.

markup
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import ListView
from .models import Book


class BookListView(LoginRequiredMixin, ListView):
    model = Book
{# 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. #}

LoginRequiredMixin से Login चाहिए बनाना

LoginRequiredMixin anonymous users को login page पर redirect करता है (settings में LOGIN_URL द्वारा, या mixin पर login_url द्वारा defined) इससे पहले कि view logic बिल्कुल भी run हो।

उदाहरण: Requiring Login with LoginRequiredMixin

An anonymous request to BookDetailView is redirected to /accounts/login/ before DetailView ever fetches the object.

markup
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import DetailView
from .models import Book


class BookDetailView(LoginRequiredMixin, DetailView):
    model = Book
    login_url = '/accounts/login/'
{# 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. #}

PermissionRequiredMixin से एक Permission चाहिए बनाना

PermissionRequiredMixin जांचता है कि logged-in user के पास access allow करने से पहले एक specific permission string है या नहीं, अगर नहीं है तो एक 403 Forbidden response return करते हुए।

उदाहरण: Requiring a Permission with PermissionRequiredMixin

Only users holding the "library.delete_book" permission can reach BookDeleteView; everyone else gets a 403.

markup
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.views.generic.edit import DeleteView
from .models import Book


class BookDeleteView(PermissionRequiredMixin, DeleteView):
    model = Book
    permission_required = 'library.delete_book'
{# 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. Class definition में mixin order गलत करना — mixins को parentheses में base generic view से पहले आना चाहिए।
  2. यह भूल जाना कि LoginRequiredMixin को anonymous users को redirect करने के लिए कहां जाना है यह जानने के लिए login_url या एक default LOGIN_URL setting चाहिए।
  3. ऐसे mixins combine करना जो दोनों same method (जैसे get_queryset) define करते हैं बिना यह जांचे कि MRO के through असल में कौन सा जीतता है।
चैप्टर सारांश
  • एक mixin एक छोटी reusable class है जो multiple inheritance के through एक class-based view में behavior जोड़ती है।
  • LoginRequiredMixin anonymous users को block करता है और उन्हें login page पर redirect करता है।
  • PermissionRequiredMixin उन users को block करता है जिनके पास एक specific permission नहीं है।
  • Mixins base generic view class से पहले listed होते हैं, क्योंकि Python methods को left to right resolve करता है।

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.