URLs are one of the most important parts of any web application. They define how users navigate your website and how Django knows which code to execute when a request is made.
In this tutorial, you will learn how Django handles URLs, how routing works, how to define URL patterns, how to use dynamic parameters, and how to organize URLs in a clean and scalable way.
URL routing is the process of matching a URL requested by the user to a specific view (function or class) in your Django application.
When a user visits:
Django needs to know:
This is exactly what URL routing does.
Let’s simplify the flow:
urls.pySo URLs are the entry point of your application.
urls.py)Every Django project has a main URL file:
config/urls.pyExample:
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]path() defines a route'admin/' is the URLadmin.site.urls is the view (admin interface)So visiting /admin/ opens the Django admin panel.
path() FunctionThe path() function is used to define URL patterns.
path(route, view, name=None)path('blog/', views.home)This means:
/blog/ → calls views.homeIt is best practice to create a separate urls.py inside each app.
blog/urls.pyfrom django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]config/urls.pyfrom django.urls import path, include
urlpatterns = [
path('blog/', include('blog.urls')),
]/blog/ → handled by blog.urls'' inside blog → calls home viewinclude()?include() allows you to connect app URLs to the main project.
path('blog/', include('blog.urls'))This means:
/blog/ go to blog.urlsSometimes URLs contain dynamic data.
/blog/5/
/blog/10/Here, 5 and 10 are dynamic values.
path('blog/<int:id>/', views.post_detail)<int:id> → captures an integerdef post_detail(request, id):
return HttpResponse(f"Post ID: {id}")/blog/5/ → shows "Post ID: 5"Django provides several converters:
<int:id> → integer<str:name> → string<slug:slug> → URL-friendly string<uuid:id> → UUID<path:path> → full pathpath('user/<str:username>/', views.profile)You can name URLs to use them later in templates and views.
path('', views.home, name='home')Instead of writing:
/blog/you use:
<a href="{% url 'home' %}">Home</a>Django allows you to generate URLs dynamically using names.
<a href="{% url 'home' %}">Home</a>from django.urls import reverse
reverse('home')This improves flexibility and code quality.
As your project grows, URL organization becomes important.
urls.pyinclude() in main URLsconfig/urls.py
blog/urls.py
users/urls.py
shop/urls.pyYou can organize URLs in a hierarchical way.
/blog/
/blog/1/
/blog/1/edit/path('blog/', include('blog.urls'))Inside blog/urls.py:
urlpatterns = [
path('', views.home),
path('<int:id>/', views.detail),
path('<int:id>/edit/', views.edit),
]Django usually uses trailing slashes:
/blog/
/contact/If you forget the slash:
/blogDjango automatically redirects (if enabled).
This is controlled by:
APPEND_SLASH = Truepath() and re_path()path()re_path()from django.urls import re_path
re_path(r'^blog/(?P<id>[0-9]+)/$', views.detail)For beginners, use path() first.
Django checks URL patterns from top to bottom.
urlpatterns = [
path('blog/<int:id>/', views.detail),
path('blog/new/', views.new_post),
]
Problem:
/blog/new/ may be treated as idurlpatterns = [
path('blog/new/', views.new_post),
path('blog/<int:id>/', views.detail),
]
Always place specific routes before dynamic ones.
include()Without it, app URLs will not work.
Forgetting:
from django.urls import path, includeMay cause unexpected redirects.
URL and view parameters must match.
Leads to hardcoded and fragile URLs.
config/urls.pyurlpatterns = [
path('blog/', include('blog.urls')),
]blog/urls.pyurlpatterns = [
path('', views.home, name='home'),
path('<int:id>/', views.detail, name='detail'),
]
views.pydef home(request):
return HttpResponse("Blog Home")
def detail(request, id):
return HttpResponse(f"Post {id}")Think of URLs like a GPS system.
urls.py is the mapWhen a user enters an address, Django uses the map to find the correct destination.
In this tutorial, you learned how Django routes URLs to views using urls.py. You saw how to define routes with path(), organize URLs using include(), handle dynamic parameters, and use named URLs for flexibility.
URL routing is a fundamental concept because it connects user requests to your application logic.
urls.py defines routingpath() is used to define routesinclude() helps organize app URLs<int:id>urls.py?include() do?Django URL routing is the backbone of your application navigation. Once you understand how URLs connect to views, you can start building dynamic and interactive web applications.