Generic View के Mixins
In this page:
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.
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.
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.
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. #}
- Class definition में mixin order गलत करना — mixins को parentheses में base generic view से पहले आना चाहिए।
- यह भूल जाना कि LoginRequiredMixin को anonymous users को redirect करने के लिए कहां जाना है यह जानने के लिए
login_urlया एक defaultLOGIN_URLsetting चाहिए। - ऐसे 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 करता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: