Django Academy · درس

Mixins و as_view()

تركيب السلوك وربط CBVs بعناوين urls

الدرس 4 من 413 خطوة

Mixins و as_view() درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

What as_view Really Does

A class cannot be a URL target on its own. as_view() returns a function that creates an instance and dispatches the request.

Always Call as_view in urls

In urls, you call the class with as_view(), never the bare class. That callable is what Django actually routes to.

path("", HomeView.as_view())

Pass Attributes Through as_view

You can override class attributes right in the URL by passing them to as_view(). Handy for small tweaks without subclassing.

HomeView.as_view(template_name="alt.html")

Meet Mixins

A mixin is a small class that adds one focused behavior. You combine mixins to compose a view from reusable parts.

Mixins Are Not Standalone

A mixin is not used alone. It only makes sense when combined with a real view class through multiple inheritance.

LoginRequiredMixin

LoginRequiredMixin blocks anonymous users and redirects them to login. Add it to any CBV that needs a signed-in user.

class DashboardView(LoginRequiredMixin, TemplateView):
    template_name = "dash.html"

Order Matters in MRO

List the mixin first, then the view. Python resolves methods left to right, so the mixin can act before the base view runs.

ContextMixin Adds Data

Generic CBVs already mix in ContextMixin, which is why get_context_data exists. Mixins are everywhere in Django.

Combine Several Mixins

You can stack multiple mixins for several behaviors at once, like requiring login and adding permissions, in one class.

Keep Mixins Small

Good mixins do one thing. Small, focused mixins stay easy to reuse and combine without surprising conflicts.

CBVs Are Composed Classes

Putting it together, a generic CBV is really a stack of mixins plus a base view, all wired into urls by as_view().

Quick Check

Why must you call as_view() in your URL configuration?

Recap: Mixins and as_view

You wrapped up CBVs: as_view() turns a class into a routable callable, and mixins compose small reusable behaviors, order-sensitive, into one view. 🧩

البدء مجانًا

تعلم Python مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
30
الدروس
120

الأسئلة الشائعة

هل درس «Mixins و as_view()» مجاني؟

نعم — نص درس «Mixins و as_view()» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.

ماذا ستتعلم في «Mixins و as_view()»؟

تركيب السلوك وربط CBVs بعناوين urls تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟

لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «Mixins و as_view()»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟

نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. من Views الدالية إلى فئات View
  2. ‏TemplateView وget_context_data
  3. التعامل مع طرائق get وpost
  4. Mixins و as_view()
← العودة إلى Django Academy