Flask Academy · درس

الشرطات المائلة الختامية وسلوك إعادة التوجيه

افهم عناوين URL الأساسية وإعادة التوجيه التلقائية.

الدرس 4 من 413 خطوة

الشرطات المائلة الختامية وسلوك إعادة التوجيه درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

One Slash Changes Everything

To Flask, /about and /about/ can be two different things. That tiny trailing slash decides how a route matches and whether a redirect happens.

Route Defined With a Slash

When your route ends in a slash, it behaves like a folder. Visiting /docs/ works, and /docs gets redirected to the slashed version. 📁

@app.route('/docs/')
def docs():
    return 'Docs'

Route Defined Without a Slash

A route with no trailing slash behaves like a file. Here /docs works, but /docs/ returns a 404 instead of redirecting.

@app.route('/docs')
def docs():
    return 'Docs'

The Folder-Style Redirect

For folder-style routes, Flask helpfully redirects the slashless URL to the canonical one. This keeps a single canonical address for that page.

No Redirect the Other Way

The kindness only flows one direction. A file-style route will not redirect from /docs/ to /docs; the extra slash simply yields a 404.

Why Canonical URLs Matter

One canonical URL per page avoids duplicate addresses. That helps caching, analytics, and SEO, since search engines see a single canonical link.

url_for Picks the Right Form

Let url_for generate links and you always get the canonical slash form. You never have to remember which routes end in a slash.

url_for('docs')  # uses the route's exact form

Pick a Convention and Hold It

Choose one style across your app, often no trailing slash for endpoints. A steady convention means fewer surprise 404s for your users.

Redirects Are a Client Round Trip

A redirect sends a 301 or 308 back, and the browser then requests the new URL. That is one extra round trip you avoid by linking correctly.

POST and the Slash Trap

Slash redirects on a POST can drop the body or change the method. Always post to the exact canonical URL to keep your data intact.

Test Both Forms

When debugging a missing page, try the URL with and without the slash. The difference often reveals a mismatch between your link and your route.

Quick Check

Your route is @app.route('/docs') with no trailing slash. What does visiting /docs/ return?

Recap: Slashes Are Decisions

A trailing slash makes a route folder-like and redirects to its canonical form; without one it is strict. Lean on url_for and stay consistent. 🎉

البدء مجانًا

تعلم Python مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
30
الدروس
120

الأسئلة الشائعة

هل درس «الشرطات المائلة الختامية وسلوك إعادة التوجيه» مجاني؟

نعم — نص درس «الشرطات المائلة الختامية وسلوك إعادة التوجيه» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.

ماذا ستتعلم في «الشرطات المائلة الختامية وسلوك إعادة التوجيه»؟

افهم عناوين URL الأساسية وإعادة التوجيه التلقائية. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟

لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «الشرطات المائلة الختامية وسلوك إعادة التوجيه»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟

نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. التقاط المتغيرات من المسار
  2. محولات الأنواع: int وfloat وstring وpath
  3. بناء عناوين URL باستخدام url_for
  4. الشرطات المائلة الختامية وسلوك إعادة التوجيه
← العودة إلى Flask Academy