รวม URL ของแอปและตั้งชื่อเส้นทาง
ใช้ include() และตั้งชื่อเส้นทางสำหรับ reverse()
รวม URL ของแอปและตั้งชื่อเส้นทาง เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Two Levels of URLs
Django splits routing into the project's urls.py and each app's own. The project includes app URLs to keep things tidy.
Why Apps Get Their Own urls.py
Giving each app its own URL file keeps routes reusable and self-contained. You can drop an app into any project cleanly.
Importing include
To delegate to an app, you use include. Import it alongside path from django.urls in the project file.
from django.urls import path, includeWiring Up include()
In the project urls.py, point a prefix at an app's URL module with include. All its routes hang under that prefix.
path('blog/', include('blog.urls'))How Prefixes Combine
The project prefix joins the app route. So blog/ plus an app route of post/ serves the full path blog/post/.
Naming a Route
Give each route a name so you never hardcode its URL. You refer to the route by this stable name everywhere else.
path('about/', views.about, name='about')Why Names Beat Hardcoding
If a URL changes, hardcoded links break. A named route lets you change the path once and every link still works.
reverse() in Python
Use reverse to build a URL from a route name in your view code. Django returns the current path for that name.
from django.urls import reverse
url = reverse('about')app_name and Namespaces
Set app_name in an app's urls.py to namespace its routes. Then names look like blog:detail, avoiding clashes between apps.
app_name = 'blog'Referencing Namespaced Routes
With a namespace set, you reference routes as namespace:name. This keeps blog:detail and shop:detail clearly separate.
reverse('blog:detail')The redirect Shortcut
The redirect helper also accepts a route name. Send users somewhere by name instead of a fragile literal path.
from django.shortcuts import redirect
return redirect('about')Quick Check
Recall what include() does in the project's urls.py.
Recap: Include and Name
Use include to mount app URLs under a prefix, and name routes so reverse and redirect stay solid when paths change. 🧭
คำถามที่พบบ่อย
บทเรียน “รวม URL ของแอปและตั้งชื่อเส้นทาง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “รวม URL ของแอปและตั้งชื่อเส้นทาง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “รวม URL ของแอปและตั้งชื่อเส้นทาง”
ใช้ include() และตั้งชื่อเส้นทางสำหรับ reverse() คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “รวม URL ของแอปและตั้งชื่อเส้นทาง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม
ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เขียนวิวแบบฟังก์ชัน
- รูปแบบ URL ด้วย path()
- จับพารามิเตอร์จาก URL
- รวม URL ของแอปและตั้งชื่อเส้นทาง