التقاط معاملات URL
اقرأ محوّلات المسار مثل int وslug داخل Views
التقاط معاملات URL درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Capture Parameters
Many pages depend on a value in the URL, like a product id. Django lets you capture that value and pass it straight to your view.
Angle Bracket Syntax
You capture a piece of the URL with angle brackets. Whatever is inside becomes a named argument for your view.
path('post/<int:id>/', views.detail)Converters Set the Type
The word before the colon is a converter. It both matches a pattern and converts the captured text to the right Python type.
The int Converter
The int converter matches digits and hands your view a real integer, not a string. Perfect for numeric ids.
path('post/<int:id>/', views.detail)The slug Converter
The slug converter matches letters, numbers, hyphens, and underscores. It is ideal for readable, SEO-friendly URLs.
path('blog/<slug:title>/', views.post)The View Receives the Value
The captured name becomes a parameter in your view, right after request. The names must match exactly.
def detail(request, id):
return HttpResponse('Post ' + str(id))Names Must Match
The name in the URL and the view argument must be identical. If the URL says id, your view must accept id too.
More Built-in Converters
Beyond int and slug, Django ships str, uuid, and path converters. Each matches a different shape of value.
str Is the Default
If you skip the converter, Django uses str. It matches any text except a slash, which keeps segments separate.
path('user/<username>/', views.profile)Capturing Multiple Values
You can capture several values in one route. Each becomes its own keyword argument in the view, in order.
path('<int:year>/<int:month>/', views.archive)Wrong Type, No Match
If a URL value does not fit the converter, the route simply does not match. Django moves on or returns a 404.
Quick Check
Consider which converter fits a numeric id.
Recap: Dynamic URLs
Use angle brackets with a converter to capture URL values. The view receives them by name, typed and ready to use. ✨
الأسئلة الشائعة
هل درس «التقاط معاملات URL» مجاني؟
نعم — نص درس «التقاط معاملات URL» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.
ماذا ستتعلم في «التقاط معاملات URL»؟
اقرأ محوّلات المسار مثل int وslug داخل Views تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟
لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «التقاط معاملات URL»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟
نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- كتابة View قائمة على دالة
- أنماط URL باستخدام path()
- التقاط معاملات URL
- تضمين عناوين URL للتطبيقات وتسمية المسارات