📖 Scenario: You are creating a simple Django web application. You need to define URL patterns that connect web addresses to views so users can navigate your site.
🎯 Goal: Build a urlpatterns list in Django that maps specific URL paths to their corresponding view functions.
📋 What You'll Learn
Create a list called urlpatterns
Include exact URL patterns using path() for given routes
Use the exact view function names provided
Import necessary functions and views correctly
💡 Why This Matters
🌍 Real World
Django URL patterns connect web addresses to the code that runs when users visit those addresses. This is essential for building any web app.
💼 Career
Understanding how to define and organize URL patterns is a key skill for Django developers working on real websites and web services.
Progress0 / 4 steps
1
Import path and create an empty urlpatterns list
Write the import statement to import path from django.urls. Then create an empty list called urlpatterns.
Django
Hint
Use from django.urls import path to import the path function.
Create urlpatterns as an empty list.
2
Import the views module
Add an import statement to import the views module from the current package.
Django
Hint
Use from . import views to import views from the current directory.
3
Add URL patterns for home and about pages
Add two path() entries to the urlpatterns list: one for the empty string '' mapped to views.home, and one for 'about/' mapped to views.about.
Django
Hint
Use path('', views.home) for the home page.
Use path('about/', views.about) for the about page.
4
Add URL pattern for contact page with a name
Add a path() entry to urlpatterns for the URL 'contact/' mapped to views.contact and give it the name 'contact'.
Django
Hint
Use the name argument in path() to name the URL pattern.
Practice
(1/5)
1. What is the main purpose of the urlpatterns list in a Django project?
easy
A. To store user data in the database
B. To connect website addresses (URLs) to the code that shows pages
C. To define the style of the website pages
D. To manage user login and authentication
Solution
Step 1: Understand the role of urlpatterns
The urlpatterns list 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 by urlpatterns.
Final Answer:
To connect website addresses (URLs) to the code that shows pages -> Option B
Quick Check:
urlpatterns connects URLs to views [OK]
Hint: Remember: urlpatterns links URLs to views [OK]
Common Mistakes:
Thinking urlpatterns stores data
Confusing urlpatterns with styling or authentication
2. Which of the following is the correct way to add a URL pattern to the urlpatterns list?
easy
A. link('home/', views.home_view)
B. url('home/', views.home_view)
C. path('home/', views.home_view)
D. route('home/', views.home_view)
Solution
Step 1: Identify the correct function for URL patterns
Django uses path() to define URL patterns in urlpatterns.
Step 2: Check other options
url() was used in older Django versions but is deprecated; route() and link() are not Django functions.
Final Answer:
path('home/', views.home_view) -> Option C
Quick Check:
Use path() for URL patterns [OK]
Hint: Use path() to add URLs in urlpatterns [OK]
Common Mistakes:
Using deprecated url() instead of path()
Trying non-existent functions like route() or link()
3. Given this 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),
]
medium
A. /contact/
B. /about-us/
C. /home/
D. /about/
Solution
Step 1: Read the urlpatterns entries
The list has path('about/', views.about_view) which means the URL /about/ shows about_view.
Step 2: Match URLs to views
/contact/ shows contact_view, others like /home/ or /about-us/ are not defined.
Final Answer:
/about/ -> Option D
Quick Check:
path('about/', views.about_view) means URL is /about/ [OK]
Hint: Match the string in path() to the URL [OK]
Common Mistakes:
Confusing URL strings with view names
Assuming URL ends without trailing slash
4. Identify the error in this urlpatterns list:
from django.urls import path
from . import views
urlpatterns = [
path('home/', views.home_view),
path('about/', views.about_view
]
medium
A. Missing closing parenthesis in second path()
B. Using views instead of view functions
C. Incorrect import statement
D. urlpatterns should be a dictionary, not a list
Solution
Step 1: Check syntax of each path() call
The second path('about/', views.about_view is missing a closing parenthesis.
Step 2: Verify other parts
Imports and list structure are correct; views is correctly used to reference functions.
Final Answer:
Missing closing parenthesis in second path() -> Option A
Quick Check:
Every path() needs matching parentheses [OK]
Hint: Count parentheses in each path() call [OK]
Common Mistakes:
Forgetting closing parenthesis
Thinking urlpatterns is a dictionary
5. You want to include URL patterns from an app called blog inside your main urlpatterns. Which is the correct way to do this?
hard
A. path('blog/', include('blog.urls'))
B. path('blog/', blog.urls)
C. include('blog.urls')
D. path('blog/', 'blog.urls')
Solution
Step 1: Understand how to include app URLs
Use include() inside path() to add app URL patterns under a prefix.
Step 2: Check other options
include('blog.urls') misses the path() wrapper; path('blog/', blog.urls) misses include(); path('blog/', 'blog.urls') passes a string instead of include().
Final Answer:
path('blog/', include('blog.urls')) -> Option A
Quick Check:
Use include() inside path() for app URLs [OK]
Hint: Use include() inside path() to add app URLs [OK]
Common Mistakes:
Forgetting to use include()
Passing string directly without include()
Not wrapping include() inside path()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.