Lesson 13 of 60 – Django URLs
22%

🔗 Django URLs

In this lesson, you will learn how URL routing works in Django. URL configuration tells Django what should happen when a user visits a particular URL.

Note: Django uses URL patterns to connect requested URLs with views. The main URL configuration is usually stored in urls.py.

1. What Is a URL?

URL stands for Uniform Resource Locator. It is the address used to access a resource on the web.

For example:

http://127.0.0.1:8000/about/

Here, /about/ identifies a particular page or resource.

2. What Is URL Routing?

URL routing is the process of matching a requested URL with the appropriate Django view.

Django checks the URL patterns and determines which view should handle the request.

3. The urls.py File

Django normally creates a urls.py file when you create a project.

myproject/
    urls.py

This file contains the main URL configuration for the project.

4. urlpatterns

Django uses a variable called urlpatterns to store URL patterns.

urlpatterns = [
]

URL patterns are added inside this list.

5. The path() Function

Django provides the path() function for defining URL patterns.

from django.urls import path

A URL pattern commonly uses path() to connect a URL to a view.

6. Basic URL Pattern

Suppose a view called home exists. A URL pattern can be written as:

path('', home)

This pattern can be used for the project's root URL.

7. Importing a View

Before using a view in urls.py, it must be available to the URL configuration.

For example:

from . import views

Then a view can be referenced as views.home.

8. First URL Example

A simple URL configuration can look like this:

from django.contrib import admin
from django.urls import path
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home),
]

Here, the empty path represents the root URL.

9. Root URL

The root URL is the main URL of the website.

For example:

http://127.0.0.1:8000/

It can be mapped using an empty path:

path('', views.home)

10. About URL

You can create a URL for an About page:

path('about/', views.about)

The corresponding browser URL would be:

http://127.0.0.1:8000/about/

11. Multiple URL Patterns

A Django project can contain many URL patterns.

urlpatterns = [
    path('', views.home),
    path('about/', views.about),
    path('contact/', views.contact),
]

Each URL can be connected to a different view.

12. URL and View Relationship

A URL pattern connects a URL to a view.

URL View
/ views.home
/about/ views.about
/contact/ views.contact

13. URL Trailing Slash

Django URL patterns commonly use a trailing slash.

path('about/', views.about)

This pattern corresponds to the URL ending with /about/.

14. URL Names

URL patterns can be given names using the name argument.

path('about/', views.about, name='about')

Naming URLs makes it easier to refer to them elsewhere in a Django project.

15. Using URL Names

Suppose a URL is named about:

path('about/', views.about, name='about')

The name can later be used with Django's URL reversing features instead of hard-coding the URL in templates or Python code.

16. URL Parameters

Django can capture values from URLs using path converters.

For example:

path('student/<int:id>/', views.student)

Here, id is captured from the URL and passed to the view.

17. Integer URL Converter

The int converter matches an integer value.

path('student/<int:id>/', views.student)

A URL such as /student/10/ can match this pattern.

18. String URL Converter

The str converter can capture a non-empty string that does not include a slash.

path('profile/<str:username>/', views.profile)

A URL such as /profile/rahul/ can match this pattern.

19. slug URL Converter

The slug converter can match ASCII letters or numbers, hyphens and underscores.

path('post/<slug:title>/', views.post)

It is useful for URL-friendly values such as article identifiers.

20. path() with a Parameter

A complete example can look like this:

path('student/<int:id>/', views.student, name='student')

The captured id value is passed to the associated view.

21. Include App URLs

Larger Django projects commonly keep application URLs in the app itself. The project's main URL configuration can include them.

from django.urls import include, path

urlpatterns = [
    path('students/', include('students.urls')),
]

22. Why Use include()?

The include() function helps organize URL patterns into separate application-level URL configuration files.

This is especially useful when a project contains multiple applications.

23. Example App urls.py

A Django app can have its own urls.py file.

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

The main project URL configuration can include these patterns.

24. Main URLs and App URLs

A common structure is:

myproject/
    urls.py

students/
    urls.py

The project's urls.py can include the URL patterns from the students app.

25. Admin URL

A new Django project normally includes a URL for the Django admin site.

path('admin/', admin.site.urls)

This maps the /admin/ URL to Django's admin site.

26. Order of URL Patterns

Django checks URL patterns in the order in which they appear in urlpatterns.

Therefore, URL patterns should be organized carefully, especially when patterns could potentially match similar URLs.

27. Complete Basic urls.py Example

from django.contrib import admin
from django.urls import path
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home'),
    path('about/', views.about, name='about'),
    path('contact/', views.contact, name='contact'),
]

This example connects three application pages and the Django admin site.

28. Test URL Changes

After changing URL patterns, run the development server and visit the corresponding URL in your browser.

python manage.py runserver

Then test the URL in the browser.

29. Common URL Mistakes

  • Forgetting to import path
  • Using an incorrect view name
  • Using an incorrect URL pattern
  • Forgetting to include an app's URLs
  • Using the wrong URL parameter type
  • Running the project with incorrect configuration

Check the terminal error message carefully when a URL does not work.

30. Django URLs Summary

Django URL routing connects browser requests with views. URL patterns are stored in urlpatterns and commonly use the path() function.

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
    path('about/', views.about, name='about'),
]

For larger projects, include() can be used to organize application URLs into separate files.

📌 Key Points

  • urls.py contains Django URL configuration.
  • urlpatterns stores URL patterns.
  • path() is commonly used to define URL patterns.
  • A URL pattern connects a URL to a view.
  • URL patterns can have names using the name argument.
  • Django supports URL parameters such as int, str and slug.
  • include() helps organize URLs for separate Django apps.
  • The order of URL patterns matters when matching requests.

🧠 Quick Quiz

Question: Which function is commonly used to define URL patterns in Django?