0Pricing
Django Academy · บทเรียน

TemplateView และ get_context_data

แสดงผลเทมเพลตและเพิ่มบริบทอย่างเป็นระเบียบ

TemplateView และ get_context_data เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 context

Add Your Own Keys

After super(), insert any keys you need into the context dict. The template reads them by name.

context["year"] = 2024
return context

Templates 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” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “TemplateView และ get_context_data”

แสดงผลเทมเพลตและเพิ่มบริบทอย่างเป็นระเบียบ คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “TemplateView และ get_context_data” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม

ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. จากวิวแบบฟังก์ชันสู่วิวแบบคลาส
  2. TemplateView และ get_context_data
  3. จัดการเมธอด get และ post
  4. มิกซ์อินและ as_view()
← กลับไปที่ Django Academy