Django Tutorial
Django Create A Complete Project
Django Template
Django Database Query
Django Form
Django Authentication and Permission Management
Django unittest
Django Advanced
In this tutorial, we'll provide an overview of the Django routing system, which allows you to map URLs to specific views in your web application. We'll go through creating a Django project, setting up routing using the path
and re_path
methods, and using URL parameters.
pip install django
django-admin startproject routing_tutorial cd routing_tutorial
python manage.py startapp myapp
routing_tutorial/settings.py
. Add 'myapp'
to the INSTALLED_APPS
list:INSTALLED_APPS = [ # ... 'myapp', ]
myapp/views.py
:from django.http import HttpResponse def index(request): return HttpResponse("Welcome to the index page!")
myapp/urls.py
. If this file doesn't exist, create it:from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), ]
routing_tutorial/urls.py
:from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('myapp/', include('myapp.urls')), ]
myapp/views.py
:def greet(request, name): return HttpResponse(f"Hello, {name}!")
myapp/urls.py
:from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), path('greet/<str:name>/', views.greet, name='greet'), ]
In this example, <str:name>
is a URL parameter that will be passed to the greet
view function. The str
part is a type converter that tells Django to expect a string, and name
is the variable name that will be passed to the view function.
re_path
method. First, import it in myapp/urls.py
:from django.urls import path, re_path
Then, you can create a URL pattern using re_path
:
re_path(r'^say_hello/(?P<name>\w+)/$', views.say_hello, name='say_hello'),
In this example, ^say_hello/
means the URL should start with "say_hello/". (?P<name>\w+)
is a named capturing group that captures one or more word characters and passes them to the view function as the name
variable. The $
symbol means the end of the URL string.
python manage.py runserver
This tutorial has shown you the basics of Django's routing system, including how to use the path
and re_path
methods and URL parameters.
Django urlpatterns and Routing:
# urls.py from django.urls import path from . import views urlpatterns = [ path('home/', views.home, name='home'), path('about/', views.about, name='about'), ]
Django Routing vs. Dispatching:
# urls.py from django.urls import path from . import views urlpatterns = [ path('articles/', views.article_list, name='article-list'), path('articles/<int:article_id>/', views.article_detail, name='article-detail'), ]
Django Views and URL Patterns:
# views.py from django.shortcuts import render def article_list(request): # Logic to retrieve and render a list of articles return render(request, 'articles/list.html') def article_detail(request, article_id): # Logic to retrieve and render details of a specific article return render(request, 'articles/detail.html', {'article_id': article_id})
Django URL Dispatching Mechanism:
# urls.py from django.urls import path, include urlpatterns = [ path('articles/', include('articles.urls')), ]
Django path()
vs re_path()
in urlpatterns:
path()
for simple string-based URL patterns and re_path()
for more complex patterns with regular expressions.# Using path() path('articles/<int:article_id>/', views.article_detail, name='article-detail') # Using re_path() re_path(r'^articles/(?P<article_id>\d+)/$', views.article_detail, name='article-detail')
Django Routing Configuration:
# urls.py from django.urls import path, include urlpatterns = [ path('articles/', include('articles.urls')), ]
Django Route Patterns and Parameters:
# urls.py path('articles/<int:year>/<int:month>/', views.article_archive, name='article-archive') # views.py def article_archive(request, year, month): # Use the captured year and month in the view logic return render(request, 'articles/archive.html', {'year': year, 'month': month})
Django include()
Function in urlpatterns:
include()
function to include other urlpatterns from different files for modularity.# urls.py from django.urls import path, include urlpatterns = [ path('articles/', include('articles.urls')), ]
Django Reverse URL Routing:
reverse()
function.# views.py from django.urls import reverse from django.shortcuts import redirect def redirect_to_home(request): # Redirect to the 'home' URL pattern dynamically home_url = reverse('home') return redirect(home_url)
Django URL namespaces and Routing:
# urls.py (project-level) from django.urls import path, include urlpatterns = [ path('articles/', include('articles.urls', namespace='articles')), ] # urls.py (app-level - articles) from django.urls import path app_name = 'articles' urlpatterns = [ path('list/', views.article_list, name='article-list'), ]
Django Dynamic URL Routing:
# urls.py path('articles/<int:article_id>/', views.article_detail, name='article-detail') # views.py def article_detail(request, article_id): # Use the captured article_id in the view logic return render(request, 'articles/detail.html', {'article_id': article_id})
Django route()
Method in urlpatterns:
route()
method in views to generate URLs for dynamic routing within the app.# views.py from django.urls import reverse from django.shortcuts import redirect def redirect_to_category(request, category_id): # Redirect to the 'category-detail' URL pattern dynamically category_url = reverse('category-detail', args=[category_id]) return redirect(category_url)
Django App-Level URL Routing:
# urls.py (app-level) from django.urls import path urlpatterns = [ path('list/', views.item_list, name='item-list'), ]