بناء عناوين URL باستخدام url_for
أنشئ الروابط من أسماء نقاط النهاية بدلًا من المسارات المكتوبة مباشرة.
بناء عناوين URL باستخدام url_for درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Hardcoded URLs Are Fragile
Typing "/user/alice" by hand breaks the day you rename a route. Flask offers url_for to build links from your view names instead.
Build by Endpoint Name
url_for takes the view function name and returns its path. Change the route string later and every generated link updates automatically. 🔗
from flask import url_for
url_for('home')The Endpoint Is the Function Name
By default the endpoint is the view function's name, not its URL path. So def home() is referenced as url_for('home').
@app.route('/')
def home():
return 'Hi'Pass Dynamic Values
For routes with captured parts, supply them as keyword arguments. url_for drops each value into the matching angle-bracket slot.
url_for('profile', name='alice')
# -> '/user/alice'Extra Args Become Query Strings
Arguments that do not fit a route slot are added as a query string. So passing page=2 appends ?page=2 to the generated URL.
url_for('search', q='cats', page=2)
# -> '/search?q=cats&page=2'Use It Inside Templates
Templates call url_for too, so your HTML links never go stale. It is the standard way to write hrefs in Jinja2 pages.
<a href="{{ url_for('home') }}">Home</a>Refactor Without Fear
Because links derive from endpoint names, you can rename a URL path freely. Every url_for call keeps working, so refactors stay safe.
Redirect Pairs Well with It
Combine url_for with redirect to send users to a named view. You get a clean, refactor-proof redirect without writing the path by hand.
from flask import redirect
redirect(url_for('home'))Absolute URLs When Needed
Pass _external=True to get a full http://host/path link. This is handy for emails or anywhere a relative path will not do.
url_for('home', _external=True)It Knows the static Folder
url_for also builds links to assets via the static endpoint. That keeps CSS and image paths correct even if your app mounts elsewhere.
url_for('static', filename='app.css')A Habit Worth Forming
Make url_for your default for every internal link. Hardcoded strings creep in and rot; generated URLs stay correct as your app evolves.
Quick Check
You call url_for('profile', name='alice', page=2). Where does page=2 end up?
Recap: You Built URLs Smartly
url_for turns endpoint names into paths, fills route variables, and tacks extras onto the query string. Next: trailing slashes. 🧩
الأسئلة الشائعة
هل درس «بناء عناوين URL باستخدام url_for» مجاني؟
نعم — نص درس «بناء عناوين URL باستخدام url_for» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.
ماذا ستتعلم في «بناء عناوين URL باستخدام url_for»؟
أنشئ الروابط من أسماء نقاط النهاية بدلًا من المسارات المكتوبة مباشرة. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟
لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «بناء عناوين URL باستخدام url_for»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟
نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- التقاط المتغيرات من المسار
- محولات الأنواع: int وfloat وstring وpath
- بناء عناوين URL باستخدام url_for
- الشرطات المائلة الختامية وسلوك إعادة التوجيه