ส่งข้อมูลเข้าเทมเพลต
ส่งตัวแปร Python ไปยังหน้าที่เรนเดอร์แล้ว
ส่งข้อมูลเข้าเทมเพลต เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flask Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Pages That Change
Static HTML is fine, but real pages show different data per user. You make pages dynamic by passing data from your view into the template.
Keyword Arguments
Add keyword arguments to render_template. Each name you pass becomes a variable you can use inside the HTML file.
return render_template('hi.html', name='Ada')Reading a Variable
Inside the template, print a value with double curly braces. Flask swaps the expression for its result when the page renders.
<h1>Hello, {{ name }}!</h1>The Expression Delimiter
Those {{ }} markers mean output this value here. Anything inside is evaluated and its result is written into the page.
Passing Many Values
You can pass as many keyword arguments as you need. Each one is independent and available throughout the template.
return render_template('p.html', name='Ada', age=36)Passing Lists
Values are not limited to text. You can hand a template a list or any Python object and work with it in the markup.
return render_template('t.html', items=['a','b','c'])Accessing Object Attributes
Use a dot to reach attributes, just like in Python. Templates read object properties the same intuitive way you do in code.
<p>{{ user.email }}</p>Dictionary Lookups
For a dictionary, use either the dot or bracket form. Jinja2 tries attribute access first, then falls back to a key lookup.
{{ data.title }} or {{ data['title'] }}Missing Values Are Safe
If a variable is undefined, Jinja2 renders nothing instead of crashing. That keeps a small typo from breaking the whole page.
Applying Filters
Transform a value on the fly with a filter, written after a pipe. Filters format text without cluttering your Python view.
<h1>{{ name|upper }}</h1>Keep Logic in the View
Do heavy work in Python and pass ready values in. Templates should display data, not compute it, which keeps pages simple.
Quick Check
How do you output a variable named title inside a Jinja2 template?
Recap
You now pass data through keyword arguments and print it with double curly braces, even reaching attributes and applying filters. Great progress! 🚀
คำถามที่พบบ่อย
บทเรียน “ส่งข้อมูลเข้าเทมเพลต” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ส่งข้อมูลเข้าเทมเพลต” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ส่งข้อมูลเข้าเทมเพลต”
ส่งตัวแปร Python ไปยังหน้าที่เรนเดอร์แล้ว คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ส่งข้อมูลเข้าเทมเพลต” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม
ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- render_template และโฟลเดอร์ templates
- ส่งข้อมูลเข้าเทมเพลต
- ลูปและเงื่อนไขของ Jinja2
- การหลีกอักขระอัตโนมัติและตัวกรอง safe