0
0
Djangoframework~10 mins

URL namespacing in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - URL namespacing
Define app URLs with app_name
Include app URLs in project URLs with namespace
Use {% url 'namespace:name' %} in templates or reverse() in views
Django resolves URL by looking up namespace and name
Correct URL returned or error if not found
This flow shows how Django uses URL namespacing to organize URLs by app and resolve them correctly.
Execution Sample
Django
app_name = 'blog'
urlpatterns = [path('post/', views.post, name='post')]

# In project urls.py
path('blog/', include(('blog.urls', 'blog'), namespace='blog'))

# In template
{% url 'blog:post' %}
Defines a URL named 'post' in the 'blog' app and uses the 'blog' namespace to refer to it.
Execution Table
StepActionInputResultNotes
1Define app_name in blog.urlsapp_name = 'blog'App URLs tagged with 'blog' namespaceSets namespace for included URLs
2Define URL patternpath('post/', views.post, name='post')URL named 'post' under 'blog' namespaceName used for reverse lookup
3Include app URLs in projectinclude(('blog.urls', 'blog'), namespace='blog')Project URLs include blog URLs under 'blog' namespaceNamespace links app URLs to project
4Use {% url 'blog:post' %} in templateRequest URL for 'blog:post'Django finds 'post' in 'blog' namespaceCorrect URL '/blog/post/' returned
5Use reverse('blog:post') in viewReverse lookup for 'blog:post'Returns '/blog/post/'Same as template URL resolution
6Use wrong namespace or namee.g. 'shop:post'NoReverseMatch errorNamespace or name must match exactly
💡 Execution stops after URL is resolved or error raised if namespace/name not found
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
app_nameundefined'blog''blog''blog''blog''blog'
urlpatternsempty[path('post/', views.post, name='post')][same][included in project URLs][same][same]
namespaceundefinedundefinedundefined'blog''blog''blog'
URL resolvedundefinedundefinedundefinedundefined'/blog/post/''/blog/post/'
Key Moments - 3 Insights
Why do we need to set app_name in the app's urls.py?
Setting app_name defines the namespace for that app's URLs. Without it, Django cannot link the included URLs to the namespace used in the project URLs (see execution_table step 1).
What happens if the namespace in include() does not match app_name?
Django will not find the URL when using the namespace:name pattern and will raise a NoReverseMatch error (see execution_table step 6). The namespace in include() must match app_name.
How does Django resolve {% url 'blog:post' %}?
Django looks up the 'blog' namespace, then finds the URL named 'post' inside it, returning the correct URL path (see execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'URL resolved' after step 4?
ANoReverseMatch error
B'/blog/post/'
C'/post/'
Dundefined
💡 Hint
Check the 'URL resolved' variable in variable_tracker after step 4
At which step does Django raise an error if the namespace or name is wrong?
AStep 2
BStep 4
CStep 6
DStep 1
💡 Hint
See execution_table row describing error on wrong namespace or name
If app_name is missing in blog.urls, what will happen when using {% url 'blog:post' %}?
ANoReverseMatch error occurs
BURL resolves correctly
CURL resolves but without namespace
DDjango ignores the namespace
💡 Hint
Refer to key_moments about importance of app_name and execution_table step 1
Concept Snapshot
URL namespacing in Django:
- Define app_name in app's urls.py
- Use include() with namespace in project urls.py
- Refer URLs as 'namespace:name' in templates and views
- Ensures URL names are unique across apps
- Missing or mismatched namespaces cause errors
Full Transcript
URL namespacing in Django helps organize URLs by app. First, you set app_name in the app's urls.py file. This tags all URLs in that file with a namespace. Then, in the project urls.py, you include the app's URLs using include() and specify the same namespace. When you want to link to a URL, you use the pattern 'namespace:name' in templates or reverse() in views. Django looks up the namespace and then the URL name to find the correct path. If the namespace or name is wrong or missing, Django raises a NoReverseMatch error. This system keeps URLs organized and avoids conflicts between apps.