The urlpatterns list tells Django which web pages to show for different website addresses. It connects web addresses to the code that shows the page.
urlpatterns list structure in Django
from django.urls import path urlpatterns = [ path('url-pattern/', view_function, name='url_name'), path('another-url/', AnotherView.as_view(), name='another_name'), ]
The urlpatterns is a Python list that holds all the URL patterns.
Each path() connects a URL pattern to a view function or class.
from django.urls import path from . import views urlpatterns = [ path('', views.home, name='home'), # Home page ]
id to show different articles.from django.urls import path from .views import ArticleDetailView urlpatterns = [ path('article/<int:id>/', ArticleDetailView.as_view(), name='article_detail'), ]
urlpatterns list means no pages are connected yet.from django.urls import path from . import views urlpatterns = [] # Empty list means no URLs are defined yet
from django.urls import path, include urlpatterns = [ path('blog/', include('blog.urls')), ]
This complete example shows a simple URL pattern that connects the root URL to a function that returns a greeting.
from django.urls import path from django.http import HttpResponse # Simple view function def hello_world(request): return HttpResponse('Hello, world!') urlpatterns = [ path('', hello_world, name='home'), ] # To test this, run Django server and visit http://localhost:8000/ to see 'Hello, world!'
The urlpatterns list is checked in order from top to bottom to find a matching URL.
Time complexity is generally fast because Django uses efficient matching behind the scenes.
Common mistake: forgetting to import the view or misspelling the URL pattern.
Use include() to keep URLs organized when your project grows.
urlpatterns is a list that connects website addresses to code that shows pages.
Use path() to add each URL pattern with a view function or class.
Keep your URLs organized by including other URL lists from apps.
Practice
urlpatterns list in a Django project?Solution
Step 1: Understand the role of
Theurlpatternsurlpatternslist holds URL patterns that link URLs to views (code that shows pages).Step 2: Compare with other options
Storing user data uses models, defining styles uses templates/CSS, and managing authentication uses Django's auth system -- none of these are handled byurlpatterns.Final Answer:
To connect website addresses (URLs) to the code that shows pages -> Option BQuick Check:
urlpatternsconnects URLs to views [OK]
- Thinking urlpatterns stores data
- Confusing urlpatterns with styling or authentication
urlpatterns list?Solution
Step 1: Identify the correct function for URL patterns
Django usespath()to define URL patterns inurlpatterns.Step 2: Check other options
url()was used in older Django versions but is deprecated;route()andlink()are not Django functions.Final Answer:
path('home/', views.home_view) -> Option CQuick Check:
Usepath()for URL patterns [OK]
path() to add URLs in urlpatterns [OK]- Using deprecated
url()instead ofpath() - Trying non-existent functions like
route()orlink()
urlpatterns list, what URL will show the about_view page?from django.urls import path
from . import views
urlpatterns = [
path('about/', views.about_view),
path('contact/', views.contact_view),
]Solution
Step 1: Read the
The list hasurlpatternsentriespath('about/', views.about_view)which means the URL/about/showsabout_view.Step 2: Match URLs to views
/contact/showscontact_view, others like/home/or/about-us/are not defined.Final Answer:
/about/ -> Option DQuick Check:
path('about/', views.about_view)means URL is /about/ [OK]
path() to the URL [OK]- Confusing URL strings with view names
- Assuming URL ends without trailing slash
urlpatterns list:from django.urls import path
from . import views
urlpatterns = [
path('home/', views.home_view),
path('about/', views.about_view
]Solution
Step 1: Check syntax of each
The secondpath()callpath('about/', views.about_viewis missing a closing parenthesis.Step 2: Verify other parts
Imports and list structure are correct;viewsis correctly used to reference functions.Final Answer:
Missing closing parenthesis in second path() -> Option AQuick Check:
Everypath()needs matching parentheses [OK]
path() call [OK]- Forgetting closing parenthesis
- Thinking urlpatterns is a dictionary
blog inside your main urlpatterns. Which is the correct way to do this?Solution
Step 1: Understand how to include app URLs
Useinclude()insidepath()to add app URL patterns under a prefix.Step 2: Check other options
include('blog.urls')misses thepath()wrapper;path('blog/', blog.urls)missesinclude();path('blog/', 'blog.urls')passes a string instead ofinclude().Final Answer:
path('blog/', include('blog.urls')) -> Option AQuick Check:
Useinclude()insidepath()for app URLs [OK]
- Forgetting to use include()
- Passing string directly without include()
- Not wrapping include() inside path()
Ready to prove it?
Pass the test and this pattern moves unknown
