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

render() และบริบทของเทมเพลต

ส่งข้อมูลจากวิวไปยังเทมเพลต

render() และบริบทของเทมเพลต เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Templates Hold Your HTML

A template is an HTML file with little placeholders. Your view fills those blanks with real data before it reaches the browser. 🎨

Meet the Context

The context is just a Python dictionary your view hands to the template. Its keys become the variables you can print inside the page.

context = {"name": "Ada", "count": 3}

render() Ties It Together

The render() shortcut takes the request, a template name, and a context, then returns a finished HttpResponse for you.

from django.shortcuts import render

A Tiny View That Renders

Here a view passes one value to a template. Notice how render() does all the loading and response work in a single line.

def hello(request):
    return render(request, "hello.html", {"name": "Ada"})

Printing a Variable

Inside the template you print a context value with double braces. Django swaps {{ name }} for whatever your view sent.

<h1>Hello, {{ name }}!</h1>

Where Templates Live

Django looks for templates in a templates folder, usually one per app. Keeping them there lets the loader find each file by name.

myapp/templates/myapp/hello.html

Namespacing Avoids Clashes

Nesting templates under an app-named subfolder is a namespace. It stops two apps from fighting over the same file name.

templates/blog/post.html

Dotted Access in Templates

The template language uses a dot for everything: dictionary keys, object attributes, and list items all read as value.key.

<p>{{ user.email }}</p>

Missing Keys Stay Quiet

If a context variable is missing, Django prints an empty string instead of crashing. This silent failure keeps pages from breaking on small gaps.

Passing Several Values

Your context can hold as many keys as you like, mixing strings, numbers, lists, and objects. The template reads each by its key.

return render(request, "page.html", {"posts": posts, "total": 5})

Templates Stay Logic-Light

Do heavy work in the view, not the page. The template should mostly display data, keeping presentation and logic comfortably separate.

Quick Check

Think about how data travels from a view to a page.

Recap: View to Page

You learned that render() joins a request, a template, and a context dict into one response, and that {{ }} prints those values. Next up: tags and filters.

คำถามที่พบบ่อย

บทเรียน “render() และบริบทของเทมเพลต” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “render() และบริบทของเทมเพลต”

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

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

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

บทเรียน “render() และบริบทของเทมเพลต” ใช้เวลานานแค่ไหน

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

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

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

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

  1. render() และบริบทของเทมเพลต
  2. แท็กและตัวกรองของเทมเพลต
  3. การสืบทอดเทมเพลตด้วย extends และ block
  4. แท็ก url และแท็ก static
← กลับไปที่ Django Academy