Bird
Raised Fist0
Djangoframework~20 mins

Including app URLs in Django - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
URL Inclusion 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 inclusion?

Given the following urls.py setup, what URL pattern will match the path /blog/post/5/?

Django
from django.urls import path, include

# project urls.py
urlpatterns = [
    path('blog/', include('blog.urls')),
]

# blog/urls.py
from django.urls import path

urlpatterns = [
    path('post/<int:id>/', lambda request, id: id, name='post-detail'),
]
A/blog/post/5/ does not match any URL pattern
B/post/5/ matches and calls the view with id=5
C/blog/post/5/ matches but id is passed as a string '5'
D/blog/post/5/ matches and calls the view with id=5
Attempts:
2 left
💡 Hint

Remember how include() works with URL prefixes in Django.

📝 Syntax
intermediate
1:30remaining
Which option correctly includes app URLs in Django?

Choose the correct way to include the URLs from an app named shop inside the project's urls.py.

Apath('shop/', include(shop.urls))
Bpath('shop/', include('shop.urls.py'))
Cpath('shop/', include('shop.urls'))
Dpath('shop/', include('shop'))
Attempts:
2 left
💡 Hint

The include() function expects a string with the module path to the app's urls.py.

🔧 Debug
advanced
2:00remaining
Why does this URL inclusion cause an error?

Given this urls.py snippet, why does Django raise an error when starting the server?

Django
from django.urls import path, include

urlpatterns = [
    path('store/', include('store.urls')),
    path('store/', include('store.urls')),
]
ADuplicate URL patterns with the same prefix cause a conflict error
BThe <code>include()</code> function cannot be called twice in the same file
CMissing trailing slash in the path causes the error
DDjango requires app URLs to be included only once per project
Attempts:
2 left
💡 Hint

Think about what happens if two URL patterns match the same path prefix.

❓ state_output
advanced
2:00remaining
What is the value of request.resolver_match.app_name after including app URLs?

In the project urls.py, the app URLs are included as:

path('forum/', include(('forum.urls', 'forum'), namespace='forum'))

Inside a view called by /forum/thread/10/, what is the value of request.resolver_match.app_name?

Aforum.urls
Bforum
CNone
Dthread
Attempts:
2 left
💡 Hint

Check how the include() function sets the app_name when given as a tuple.

🧠 Conceptual
expert
2:30remaining
What happens if you include app URLs without a trailing slash in the path?

Consider this project urls.py snippet:

path('api', include('api.urls'))

What is the effect on URL matching and user requests?

AURLs must start with '/api' exactly, so '/api' matches but '/api/' does not
BDjango automatically adds a trailing slash, so both '/api' and '/api/' work
CRequests to '/api/' will redirect to '/api' automatically
DNo URLs under 'api' will match because the path is missing a trailing slash
Attempts:
2 left
💡 Hint

Think about how Django treats trailing slashes in path() definitions.

Practice

(1/5)
1. What is the main purpose of including app URLs in a Django project?
easy
A. To speed up the server response time
B. To connect the database with the views
C. To automatically generate HTML templates
D. To organize URL patterns by app for better project structure

Solution

  1. Step 1: Understand URL organization in Django

    Including app URLs helps keep URL patterns separated by app, making the project easier to manage.
  2. Step 2: Identify the main benefit

    This separation improves project structure and maintainability, not server speed or template generation.
  3. Final Answer:

    To organize URL patterns by app for better project structure -> Option D
  4. Quick Check:

    URL organization = To organize URL patterns by app for better project structure [OK]
Hint: Including app URLs organizes routes by app for clarity [OK]
Common Mistakes:
  • Thinking it improves server speed
  • Confusing URL inclusion with template creation
  • Assuming it connects database automatically
2. Which of the following is the correct syntax to include app URLs under the prefix 'blog/' in the main urls.py?
easy
A. include('blog.urls', path='blog/')
B. path('blog/', include('blog.urls'))
C. path('blog/', url('blog.urls'))
D. include(path('blog/'), 'blog.urls')

Solution

  1. Step 1: Recall Django URL inclusion syntax

    The correct way is to use path('prefix/', include('app.urls')).
  2. Step 2: Match syntax with options

    path('blog/', include('blog.urls')) matches the correct syntax exactly; others misuse include or path functions.
  3. Final Answer:

    path('blog/', include('blog.urls')) -> Option B
  4. Quick Check:

    Correct include syntax = path('blog/', include('blog.urls')) [OK]
Hint: Use path('prefix/', include('app.urls')) format [OK]
Common Mistakes:
  • Swapping order of path and include
  • Using url() instead of include()
  • Passing path as argument to include
3. Given the main urls.py includes path('shop/', include('shop.urls')) and the app shop/urls.py has path('items/', views.items_list), what URL path will call items_list view?
medium
A. /shop/items/
B. /items/
C. /shop/
D. /shop/items_list/

Solution

  1. Step 1: Combine main and app URL prefixes

    Main URL adds 'shop/' prefix, app URL adds 'items/' path.
  2. Step 2: Form full URL path

    Joining them gives '/shop/items/' which calls the items_list view.
  3. Final Answer:

    /shop/items/ -> Option A
  4. Quick Check:

    Combined URL = /shop/items/ [OK]
Hint: Combine main prefix + app path for full URL [OK]
Common Mistakes:
  • Using only app path ignoring main prefix
  • Confusing view name with URL path
  • Adding extra slashes or missing trailing slash
4. What is wrong with this main urls.py snippet?
from django.urls import path
urlpatterns = [
    path('blog/', 'blog.urls'),
]
medium
A. Missing include() function around 'blog.urls'
B. Should import url instead of path
C. The string 'blog.urls' should be a list
D. The path prefix 'blog/' is invalid

Solution

  1. Step 1: Check how to include app URLs

    App URLs must be included using include('app.urls'), not just a string.
  2. Step 2: Identify missing include()

    The code uses a string directly without include(), causing an error.
  3. Final Answer:

    Missing include() function around 'blog.urls' -> Option A
  4. Quick Check:

    Include required for app URLs = Missing include() function around 'blog.urls' [OK]
Hint: Always wrap app URLs with include() [OK]
Common Mistakes:
  • Passing string directly without include()
  • Confusing path and url imports
  • Thinking prefix 'blog/' is invalid
5. You want to include two apps, 'store' and 'accounts', in your project URLs under prefixes 'shop/' and 'user/' respectively. Which is the correct way to do this in the main urls.py?
hard
A. path('shop/', 'store.urls')\npath('user/', 'accounts.urls')
B. include('store.urls', prefix='shop/')\ninclude('accounts.urls', prefix='user/')
C. path('shop/', include('store.urls'))\npath('user/', include('accounts.urls'))
D. path('store/', include('shop.urls'))\npath('accounts/', include('user.urls'))

Solution

  1. Step 1: Use path() with include() for each app

    Each app URL must be included with path('prefix/', include('app.urls')).
  2. Step 2: Match prefixes and app names correctly

    path('shop/', include('store.urls'))\npath('user/', include('accounts.urls')) correctly uses 'shop/' for 'store.urls' and 'user/' for 'accounts.urls'. Others misuse include or swap names.
  3. Final Answer:

    path('shop/', include('store.urls')) path('user/', include('accounts.urls')) -> Option C
  4. Quick Check:

    Correct multiple app inclusion = path('shop/', include('store.urls'))\npath('user/', include('accounts.urls')) [OK]
Hint: Use path('prefix/', include('app.urls')) for each app [OK]
Common Mistakes:
  • Swapping prefixes and app names
  • Using include() without path()
  • Passing strings without include()
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