Bird
Raised Fist0
Djangoframework~10 mins

URL parameters with angle brackets in Django - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to capture an integer parameter named 'pk' in the URL pattern.

Django
path('item/[1]', views.item_detail, name='item-detail')
Drag options to blanks, or click blank then click option'
A<int:pk>/
B<str:pk>/
C<pk>/
Dpk/
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the type before the colon inside angle brackets.
Omitting the trailing slash after the parameter.
Using angle brackets without specifying type.
2fill in blank
medium

Complete the code to capture a string parameter named 'username' in the URL pattern.

Django
path('profile/[1]', views.profile_view, name='profile')
Drag options to blanks, or click blank then click option'
Ausername/
B<int:username>/
C<slug:username>/
D<str:username>/
Attempts:
3 left
💡 Hint
Common Mistakes
Using <int:username> which expects an integer, not a string.
Omitting angle brackets around the parameter.
Not including the trailing slash.
3fill in blank
hard

Fix the error in the URL pattern to correctly capture a slug parameter named 'post_slug'.

Django
path('blog/[1]', views.post_detail, name='post-detail')
Drag options to blanks, or click blank then click option'
A<slug:post_slug>/
B<str:post_slug>
C<post_slug>/
Dpost_slug/
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the type 'slug' before the colon.
Forgetting the trailing slash.
Using angle brackets without type.
4fill in blank
hard

Fill both blanks to capture an integer 'year' and a string 'month' in the URL pattern.

Django
path('archive/[1]/[2]', views.archive_view, name='archive')
Drag options to blanks, or click blank then click option'
A<int:year>
B<str:month>
C<slug:month>
Dmonth
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'month' without angle brackets or type.
Mixing up the types for year and month.
Omitting slashes between parameters.
5fill in blank
hard

Fill all three blanks to capture a slug 'category', an integer 'id', and a string 'action' in the URL pattern.

Django
path('manage/[1]/[2]/[3]/', views.manage_view, name='manage')
Drag options to blanks, or click blank then click option'
A<slug:category>
B<int:id>
C<str:action>
Daction
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'action' without angle brackets and type.
Mixing up parameter types.
Forgetting trailing slash at the end.

Practice

(1/5)
1. What does the angle bracket syntax <int:id> in a Django URL pattern do?
easy
A. Creates a static URL segment named 'int:id'.
B. Specifies a query parameter instead of a URL segment.
C. Defines a string variable named 'id' without type conversion.
D. Captures a part of the URL as an integer variable named 'id'.

Solution

  1. Step 1: Understand angle brackets in Django URLs

    Angle brackets capture parts of the URL as variables to pass to views.
  2. Step 2: Recognize the converter and variable name

    The syntax <int:id> captures an integer named 'id'.
  3. Final Answer:

    Captures a part of the URL as an integer variable named 'id'. -> Option D
  4. Quick Check:

    URL parameter with <int:id> = integer variable capture [OK]
Hint: Angle brackets capture URL parts as typed variables [OK]
Common Mistakes:
  • Thinking it creates a static URL segment
  • Confusing query parameters with URL parameters
  • Ignoring the type converter like int or str
2. Which of the following is the correct syntax to capture a slug parameter named 'post_slug' in a Django URL pattern?
easy
A. path('post/<str:post_slug>/', views.post_detail)
B. path('post/<slug:post_slug>/', views.post_detail)
C. path('post/<post_slug:slug>/', views.post_detail)
D. path('post/<slug>post_slug/', views.post_detail)

Solution

  1. Step 1: Identify correct converter and variable order

    The correct syntax is <converter:variable_name>, so <slug:post_slug> is correct.
  2. Step 2: Check the full path syntax

    path('post/<slug:post_slug>/', views.post_detail) uses path('post/<slug:post_slug>/', views.post_detail), which is valid Django syntax.
  3. Final Answer:

    path('post/<slug:post_slug>/', views.post_detail) -> Option B
  4. Quick Check:

    Converter before variable name = path('post/<slug:post_slug>/', views.post_detail) [OK]
Hint: Use <converter:variable> format for URL parameters [OK]
Common Mistakes:
  • Swapping converter and variable name
  • Missing colon between converter and variable
  • Using invalid syntax like <slug>post_slug
3. Given this URL pattern: path('user/<int:user_id>/', views.profile), what will user_id be if the URL is /user/42/?
medium
A. A slug '42'
B. The string '42'
C. The integer 42
D. A syntax error occurs

Solution

  1. Step 1: Understand the int converter effect

    The <int:user_id> converter converts the URL segment to an integer.
  2. Step 2: Apply to the URL segment '42'

    The string '42' from the URL is converted to the integer 42 before passing to the view.
  3. Final Answer:

    The integer 42 -> Option C
  4. Quick Check:

    int converter converts URL part to integer [OK]
Hint: int converter makes URL part an integer, not string [OK]
Common Mistakes:
  • Assuming the parameter is a string
  • Confusing slug with int converter
  • Expecting a syntax error for valid syntax
4. Identify the error in this Django URL pattern: path('article/<int:id>', views.detail)
medium
A. Missing closing angle bracket '>' after 'id'.
B. Using 'int' converter instead of 'str'.
C. View function 'detail' is not imported.
D. URL pattern should not use angle brackets.

Solution

  1. Step 1: Check angle bracket syntax

    The pattern uses <int:id> without the closing '>' character, which is invalid syntax.
  2. Step 2: Confirm correct syntax

    The correct syntax requires both opening and closing angle brackets: <int:id>.
  3. Final Answer:

    Missing closing angle bracket '>' after 'id'. -> Option A
  4. Quick Check:

    Angle brackets must be complete <int:id> [OK]
Hint: Always close angle brackets in URL parameters [OK]
Common Mistakes:
  • Forgetting the closing '>'
  • Confusing converter types
  • Assuming view import errors cause syntax issues
5. You want to create a URL pattern that captures a username (letters, numbers, underscores) and a post ID (integer). Which of these is the best Django URL pattern?
hard
A. path('profile/<slug:username>/post/<int:post_id>/', views.post_view)
B. path('profile/<str:username>/post/<int:post_id>/', views.post_view)
C. path('profile/<int:username>/post/<int:post_id>/', views.post_view)
D. path('profile/<username>/post/<post_id>/', views.post_view)

Solution

  1. Step 1: Choose correct converter for username

    Usernames usually contain letters, numbers, underscores, so 'slug' converter fits best.
  2. Step 2: Confirm integer converter for post ID

    Post ID should be an integer, so <int:post_id> is correct.
  3. Step 3: Evaluate options

    path('profile/<slug:username>/post/<int:post_id>/', views.post_view) uses <slug:username> and <int:post_id>, matching requirements perfectly.
  4. Final Answer:

    path('profile/<slug:username>/post/<int:post_id>/', views.post_view) -> Option A
  5. Quick Check:

    Slug for username + int for post ID = path('profile/<slug:username>/post/<int:post_id>/', views.post_view) [OK]
Hint: Use slug for usernames, int for numeric IDs in URLs [OK]
Common Mistakes:
  • Using str converter which allows spaces
  • Using int converter for username
  • Omitting converters causing string default
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard