TemplateView وget_context_data
اعرض قالبًا وأضف السياق بطريقة واضحة
TemplateView وget_context_data درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Meet TemplateView
TemplateView is a generic CBV whose only job is to render a template. It is the simplest class-based view you will use.
Set template_name
You tell TemplateView which file to render with the template_name attribute. No get method needed for a plain page.
class AboutView(TemplateView):
template_name = "about.html"Wire It Into urls
Add it to urls with as_view(), just like any CBV. That one line gives you a fully working page.
path("about/", AboutView.as_view())Passing Data to Templates
A static page is fine, but usually you need context: data your template can display. CBVs provide a clean hook for that.
Override get_context_data
The get_context_data method builds the dictionary passed to your template. Override it to add your own values.
Always Call super() First
Start by calling super() to get the default context, then add to it. Skipping this can drop data Django expects.
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return contextAdd Your Own Keys
After super(), insert any keys you need into the context dict. The template reads them by name.
context["year"] = 2024
return contextTemplates Read Context Keys
In the template, reference a context key with double braces. Whatever you put under year appears where you write it.
kwargs Carries URL Data
The kwargs argument holds values captured from the URL, like an id. You can read them while building context.
Less Boilerplate Than Functions
Compared to a function view, TemplateView removes the repetitive render call. You declare the template once and add only the data.
A Common First CBV
TemplateView plus get_context_data is the pattern you will reach for whenever a page needs a template and a bit of dynamic data.
Quick Check
Which method do you override to add data for a TemplateView?
Recap: TemplateView
You learned TemplateView: set template_name to render a page, and override get_context_data, calling super first, to pass data along. ✨
الأسئلة الشائعة
هل درس «TemplateView وget_context_data» مجاني؟
نعم — نص درس «TemplateView وget_context_data» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.
ماذا ستتعلم في «TemplateView وget_context_data»؟
اعرض قالبًا وأضف السياق بطريقة واضحة تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟
لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «TemplateView وget_context_data»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟
نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- من Views الدالية إلى فئات View
- TemplateView وget_context_data
- التعامل مع طرائق get وpost
- Mixins و as_view()