Bird
Raised Fist0
Djangoframework~20 mins

path function for routes in Django - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Django Path Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this Django URL pattern matching?
Given the following URL pattern and a request to '/blog/42/', what view will be called and what will be the value of the captured parameter?
Django
from django.urls import path
from . import views

urlpatterns = [
    path('blog/<int:post_id>/', views.post_detail, name='post_detail'),
]

# Assume views.post_detail is defined to accept post_id as argument.
AThe view 'post_detail' is called with post_id='blog/42/'
BThe view 'post_detail' is called with post_id='42/'
CNo view is called because the URL does not match
DThe view 'post_detail' is called with post_id=42
Attempts:
2 left
💡 Hint
Look at how the path converter works in the URL pattern.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a path with a string parameter named 'username'?
Select the correct Django path function usage to capture a string parameter 'username' from the URL '/user//'
Apath('user/<str:username>/', views.profile, name='profile')
Bpath('user/<username>/', views.profile, name='profile')
Cpath('user/<int:username>/', views.profile, name='profile')
Dpath('user/<slug:username>/', views.profile, name='profile')
Attempts:
2 left
💡 Hint
Remember the syntax for path converters in Django's path function.
🔧 Debug
advanced
2:00remaining
Why does this URL pattern cause a runtime error?
Examine the following urlpatterns list and identify why it causes an error when Django starts.
Django
from django.urls import path

urlpatterns = [
    path('article/<int:id>/', views.article_detail),
    path('article/<int:id>/', views.article_edit),
]
AMissing 'name' argument in path causes runtime error.
BThe views module is not imported, causing NameError.
CDuplicate URL patterns with the same path cause ambiguity and Django raises an error.
DThe variable name 'id' is reserved and causes error.
Attempts:
2 left
💡 Hint
Check if all referenced modules are imported.
❓ state_output
advanced
2:00remaining
What is the value of 'kwargs' passed to the view for this path?
Given the URL pattern and a request to '/shop/electronics/123/', what is the kwargs dictionary passed to the view?
Django
path('shop/<slug:category>/<int:item_id>/', views.item_detail, name='item_detail')
A{'category': 'electronics', 'item_id': 123}
B{'category': 'electronics/123', 'item_id': None}
C{'category': 'shop', 'item_id': 'electronics'}
D{} (empty dictionary)
Attempts:
2 left
💡 Hint
Look at how multiple path converters capture parts of the URL.
🧠 Conceptual
expert
3:00remaining
Which option best explains the difference between path() and re_path() in Django routing?
Choose the most accurate explanation about the difference between path() and re_path() functions in Django URL configuration.
Apath() automatically escapes special characters; re_path() does not.
Bpath() supports only static URLs; re_path() supports dynamic URLs with parameters.
Cpath() uses simpler, readable converters for common patterns; re_path() uses full regular expressions for complex matching.
Dpath() is deprecated and replaced by re_path() in modern Django versions.
Attempts:
2 left
💡 Hint
Think about the syntax and flexibility differences between path and re_path.

Practice

(1/5)
1. What is the main purpose of the path function in Django?
easy
A. To run background tasks
B. To connect a URL pattern to a view function or class
C. To style HTML pages
D. To create a database model

Solution

  1. Step 1: Understand the role of URL routing

    Django uses URL routing to decide which code runs when a user visits a URL.
  2. Step 2: Identify what path does

    The path function links a URL pattern to a view function or class that handles the request.
  3. Final Answer:

    To connect a URL pattern to a view function or class -> Option B
  4. Quick Check:

    URL routing = connect URL to view [OK]
Hint: Remember: path links URLs to code that runs [OK]
Common Mistakes:
  • Confusing path with database models
  • Thinking path styles HTML
  • Assuming path runs background tasks
2. Which of the following is the correct syntax to define a route using path in Django?
easy
A. path('home/', views.home_view, name='home')
B. path = 'home/' views.home_view
C. path('home/' views.home_view')
D. path('home', views.home_view, 'home')

Solution

  1. Step 1: Recall the path function signature

    The path function takes at least two arguments: the URL pattern string and the view function. Optionally, a name argument can be added.
  2. Step 2: Check each option's syntax

    path('home/' views.home_view') misses comma between arguments, invalid syntax. path = 'home/' views.home_view is invalid syntax. path('home/', views.home_view, name='home') correctly uses path('home/', views.home_view, name='home'). path('home', views.home_view, 'home') has incorrect argument order and missing keyword.
  3. Final Answer:

    path('home/', views.home_view, name='home') -> Option A
  4. Quick Check:

    Correct syntax includes URL, view, and optional name [OK]
Hint: Use path('url/', view, name='route_name') format [OK]
Common Mistakes:
  • Omitting commas between arguments
  • Missing quotes around URL string
  • Passing name without keyword
3. Given the following URL pattern:
path('article//', views.article_detail, name='article_detail')

What URL will match this pattern?
medium
A. /article/42/
B. /article/abc/
C. /article/42
D. /article//

Solution

  1. Step 1: Understand the dynamic part in the URL pattern

    The pattern uses <int:id> which means it expects an integer value in that part of the URL.
  2. Step 2: Match each option to the pattern

    /article/42/ has an integer 42 and ends with a slash, matching the pattern exactly. /article/abc/ has letters 'abc' which is not an integer. /article/42 misses the trailing slash. /article// has no value where integer is expected.
  3. Final Answer:

    /article/42/ -> Option A
  4. Quick Check:

    Integer in URL with trailing slash matches [OK]
Hint: Dynamic parts need correct type and trailing slash [OK]
Common Mistakes:
  • Ignoring trailing slash
  • Using wrong data type in URL
  • Leaving dynamic part empty
4. Identify the error in this Django URL pattern:
path('profile/<username>/', views.profile_view, name='profile')
medium
A. Missing comma between arguments
B. Incorrect view function name
C. Missing type converter for <username>
D. URL pattern should not have dynamic parts

Solution

  1. Step 1: Check the dynamic part syntax

    Django requires a type converter like str or int inside angle brackets for dynamic parts, e.g., <str:username>.
  2. Step 2: Analyze the given pattern

    The pattern uses <username> without a type converter, which is invalid syntax.
  3. Final Answer:

    Missing type converter for <username> -> Option C
  4. Quick Check:

    Dynamic parts need type like str or int [OK]
Hint: Always add type before variable in < > [OK]
Common Mistakes:
  • Leaving out type converter in dynamic URL parts
  • Forgetting commas between arguments
  • Assuming any string works as dynamic part
5. You want to create a route that matches URLs like /blog/2024/06/ where the year and month are dynamic integers. Which of the following path definitions is correct?
hard
A. path('blog/<year>/<month>/', views.blog_archive, name='blog_archive')
B. path('blog///', views.blog_archive, name='blog_archive')
C. path('blog//<month>/', views.blog_archive, name='blog_archive')
D. path('blog///', views.blog_archive, name='blog_archive')

Solution

  1. Step 1: Understand the correct syntax for dynamic URL parts

    The correct format is <type:variable>, for example <int:year>.
  2. Step 2: Evaluate each option

    path('blog///', views.blog_archive, name='blog_archive') uses correct syntax for both year and month as integers. path('blog///', views.blog_archive, name='blog_archive') reverses type and variable order, which is invalid. path('blog//<month>/', views.blog_archive, name='blog_archive') misses type for month. path('blog/<year>/<month>/', views.blog_archive, name='blog_archive') misses types for both year and month.
  3. Final Answer:

    path('blog/<int:year>/<int:month>/', views.blog_archive, name='blog_archive') -> Option D
  4. Quick Check:

    Use <int:var> for integer dynamic parts [OK]
Hint: Use for integer parts in URL [OK]
Common Mistakes:
  • Swapping type and variable order
  • Omitting type converters
  • Using unknown types
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