📖 Scenario: You are building a Django website with two apps: blog and shop. Each app has its own set of URLs. To keep URLs organized and avoid conflicts, you will use URL namespacing.
🎯 Goal: Create URL configurations for the blog and shop apps with namespaces, then include them in the main project urls.py. This will allow you to refer to URLs by their namespace and name.
📋 What You'll Learn
Create a blog/urls.py file with a URL pattern named post_list.
Create a shop/urls.py file with a URL pattern named product_list.
Add a namespace blog for the blog URLs and shop for the shop URLs.
Include both app URL configurations in the main project/urls.py with their namespaces.
💡 Why This Matters
🌍 Real World
Websites often have multiple apps or sections. Namespacing URLs helps keep them organized and prevents name clashes.
💼 Career
Understanding URL namespacing is essential for Django developers to build scalable and maintainable web applications.
Progress0 / 4 steps
1
Create blog app URL patterns
Create a list called urlpatterns in blog/urls.py with one URL pattern. Use path to map the empty string '' to a view called post_list. Name this URL pattern 'post_list'.
Django
Hint
Use path('', views.post_list, name='post_list') inside urlpatterns.
2
Create shop app URL patterns
Create a list called urlpatterns in shop/urls.py with one URL pattern. Use path to map the empty string '' to a view called product_list. Name this URL pattern 'product_list'.
Django
Hint
Use path('', views.product_list, name='product_list') inside urlpatterns.
3
Add namespaces in main urls.py
In the main project/urls.py, import include and path from django.urls. Create a list called urlpatterns that includes the blog URLs with namespace 'blog' and the shop URLs with namespace 'shop'. Use path('blog/', include(('blog.urls', 'blog'), namespace='blog')) and path('shop/', include(('shop.urls', 'shop'), namespace='shop')).
Django
Hint
Use include with a tuple of (app.urls, app_name) and namespace.
4
Use namespaced URLs in templates
In a Django template, write two links using the {% url %} tag. One link should use the namespaced URL 'blog:post_list' and the other 'shop:product_list'. Use anchor tags with href attributes set to these URLs.
Django
Hint
Use {% url 'namespace:url_name' %} inside href attributes.
Practice
(1/5)
1. What is the main purpose of URL namespacing in Django?
easy
A. To speed up the loading time of web pages
B. To encrypt URL parameters for security
C. To automatically generate HTML templates
D. To group URLs under a common name and avoid conflicts between apps
Solution
Step 1: Understand URL namespacing concept
URL namespacing groups URLs under a common name to organize them and avoid conflicts.
Step 2: Identify the main purpose
This helps when multiple apps have URLs with the same name, preventing clashes.
Final Answer:
To group URLs under a common name and avoid conflicts between apps -> Option D
Quick Check:
URL namespacing = grouping URLs to avoid conflicts [OK]
Hint: Remember: namespaces keep URL names unique across apps [OK]
Common Mistakes:
Thinking it speeds up page loading
Confusing with template generation
Assuming it encrypts URLs
2. Which of the following is the correct way to include a namespaced URL in Django's main urls.py?
easy
A. path('blog/', include(('blog.urls', 'blog'), namespace='blog'))
B. path('blog/', include('blog.urls'))
C. path('blog/', include('blog.urls', app_name='blog'))
D. path('blog/', include('blog.urls', name='blog'))
Solution
Step 1: Recall include() syntax for namespacing
To namespace URLs, use include() with the namespace argument in main urls.py.
Step 2: Identify correct syntax
Correct syntax is include(('app.urls', 'appname'), namespace='appname'). The app_name is set inside app's urls.py.
Final Answer:
path('blog/', include(('blog.urls', 'blog'), namespace='blog')) -> Option A
Quick Check:
include() with namespace='name' is correct [OK]
Hint: Use namespace in include(), app_name inside app urls.py [OK]
You try to reverse 'userapp:profile' but get NoReverseMatch error. What is the likely cause?
medium
A. Missing app_name = 'userapp' in userapp.urls
B. Incorrect path prefix in main urls.py
C. Wrong view function name in urls.py
D. No namespace argument in include() in main urls.py
Solution
Step 1: Check app_name declaration in app urls.py
For namespacing to work, app's urls.py must define app_name = 'userapp'.
Step 2: Understand cause of NoReverseMatch
Without app_name, Django cannot resolve 'userapp:profile' causing NoReverseMatch.
Final Answer:
Missing app_name = 'userapp' in userapp.urls -> Option A
Quick Check:
app_name missing = NoReverseMatch for namespace [OK]
Hint: Always set app_name in app urls.py for namespacing [OK]
Common Mistakes:
Forgetting app_name in app urls.py
Assuming namespace in include() is enough
Confusing path prefix with namespace
5. You have two apps, 'blog' and 'shop', both with a URL named 'detail'. How do you configure URL namespacing so you can reverse both URLs without conflict?
hard
A. Rename one URL name to avoid conflict without using namespaces
B. Set app_name in each app's urls.py and use different namespaces in main urls.py include()
C. Use the same namespace for both apps in main urls.py
D. Use path converters to differentiate URLs instead of namespaces
Solution
Step 1: Understand URL name conflicts
Both apps have 'detail' URL name, which causes conflicts when reversing URLs.
Step 2: Apply namespacing correctly
Set app_name in each app's urls.py and assign unique namespaces in main urls.py include() calls.
Step 3: Result of correct namespacing
This allows reversing URLs as 'blog:detail' and 'shop:detail' without conflict.
Final Answer:
Set app_name in each app's urls.py and use different namespaces in main urls.py include() -> Option B
Quick Check:
Unique app_name + namespace avoids URL name conflicts [OK]
Hint: Use unique app_name and namespace per app to avoid conflicts [OK]
Common Mistakes:
Using same namespace for multiple apps
Renaming URLs instead of namespacing
Relying on path converters alone
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.