แท็ก url และแท็ก static
เชื่อมเส้นทางกับทรัพยากรโดยไม่เขียนเส้นทางตายตัว
แท็ก url และแท็ก static เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Never Hardcode Paths
Typing URLs and file paths by hand breaks the moment they change. Django gives you the url and static tags to build them safely. 🔗
The url Tag Builds Routes
The url tag turns a route name into its real path. Rename the URL pattern later and every link updates automatically.
<a href="{% url 'home' %}">Home</a>Name Your Routes First
For url to work, give each pattern a name in urls.py. That name is the stable handle the template refers to.
path("about/", views.about, name="about")Passing URL Arguments
If a route needs a value, pass it after the name. Django slots the argument into the path where the converter expects it.
{% url 'post_detail' post.id %}Namespaced URL Names
When an app sets an app_name, reference routes with a prefix like blog:detail to avoid clashing with other apps.
{% url 'blog:detail' post.id %}The static Tag for Assets
The static tag builds the correct URL to a CSS, JS, or image file, even when storage paths differ in production.
{% static 'css/style.css' %}Load static First
Before using the tag you must load static near the top of the template, which pulls the tag into that file.
{% load static %}Linking a Stylesheet
Combine the pieces to link a stylesheet the safe way, so the path stays valid no matter where files are served from.
<link rel="stylesheet" href="{% static 'css/style.css' %}">Showing an Image
The same static tag works for images. Point it at a file under your static folder and Django resolves the full URL.
<img src="{% static 'img/logo.png' %}">Why static Matters
In production, assets often move to a CDN or hashed path. The static tag hides that, so your templates never need editing.
Two Tags, One Goal
Both tags share a purpose: let Django compute paths so you do not. Use url for views and static for files.
Quick Check
Pick the right tag for the right job.
Recap: Safe Links
You learned to build view paths with the url tag and asset paths with static, after loading static. That wraps up rendering HTML with data.
คำถามที่พบบ่อย
บทเรียน “แท็ก url และแท็ก static” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “แท็ก url และแท็ก static” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “แท็ก url และแท็ก static”
เชื่อมเส้นทางกับทรัพยากรโดยไม่เขียนเส้นทางตายตัว คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “แท็ก url และแท็ก static” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม
ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- render() และบริบทของเทมเพลต
- แท็กและตัวกรองของเทมเพลต
- การสืบทอดเทมเพลตด้วย extends และ block
- แท็ก url และแท็ก static