من Views الدالية إلى فئات View
لماذا توجد CBVs ومتى تستخدمها
من Views الدالية إلى فئات View درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Two Ways to Build Views
So far your views were plain functions. Django also lets you write views as classes, which package related logic together neatly.
What a Class-Based View Is
A class-based view (CBV) is just a Python class that handles a request. Django turns it into a callable for you behind the scenes.
The Repetition Problem
Function views often repeat the same shape: fetch data, build context, render a template. CBVs let you reuse that shape instead of copying it.
A Function View, For Contrast
Here is a familiar function view. It takes a request and returns a response, all in one block.
def home(request):
return render(request, "home.html")The Same View as a Class
As a class, you subclass View and define a method per HTTP verb. The logic is the same, just organized differently.
class HomeView(View):
def get(self, request):
return render(request, "home.html")Methods Map to HTTP Verbs
In a CBV, a get method handles GET requests and a post method handles POST. No more giant if-checks on request.method.
Inheritance Means Reuse
Because CBVs are classes, you can subclass and override only what changes. Shared behavior lives in a parent class once.
Django Ships Generic Views
Django includes ready-made generic views like ListView and DetailView. They cover common patterns so you write almost no code.
When Functions Still Win
For a tiny, one-off view, a function is often clearer. Reach for CBVs when logic repeats or grows complex.
Both Plug Into urls.py
Both styles get wired the same way in urls. A CBV just needs as_view() to become a callable Django can route to.
path("", HomeView.as_view())Pick the Right Tool
CBVs are not better or worse, just different. Choose based on reuse: repeated patterns favor classes, simple pages favor functions.
Quick Check
What is the main reason class-based views exist?
Recap: Functions vs Classes
You met class-based views: classes that handle requests, map methods to HTTP verbs, and reuse logic through inheritance. Great when patterns repeat. 🎯
الأسئلة الشائعة
هل درس «من Views الدالية إلى فئات View» مجاني؟
نعم — نص درس «من Views الدالية إلى فئات View» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.
ماذا ستتعلم في «من Views الدالية إلى فئات View»؟
لماذا توجد CBVs ومتى تستخدمها تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟
لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «من Views الدالية إلى فئات View»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟
نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- من Views الدالية إلى فئات View
- TemplateView وget_context_data
- التعامل مع طرائق get وpost
- Mixins و as_view()