Django Tutorial
Django Create A Complete Project
Django Template
Django Database Query
Django Form
Django Authentication and Permission Management
Django unittest
Django Advanced
Django is a powerful web framework for building web applications using Python. One of its core features is the URL routing system, which allows you to map URLs to specific views in your application. This tutorial will guide you through the basics of Django's routing system using the path
method.
pip install django
django-admin startproject myproject cd myproject
python manage.py startapp myapp
myproject/settings.py
. Add 'myapp'
to the INSTALLED_APPS
list:INSTALLED_APPS = [ # ... 'myapp', ]
myapp/views.py
:from django.http import HttpResponse def hello(request): return HttpResponse("Hello, World!")
myapp/urls.py
. If this file doesn't exist, create it:from django.urls import path from . import views urlpatterns = [ path('hello/', views.hello, name='hello'), ]
Here, we import the path
function from django.urls
and the views
module from our app. We create a URL pattern list called urlpatterns
, where each item is a path
object. In this example, the URL path 'hello/' maps to the hello
view function in the views
module.
myproject/urls.py
:from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('myapp/', include('myapp.urls')), ]
We've added the include
function to import our app's URL patterns. The path
function is used to map the 'myapp/' URL path to our app's URL patterns.
python manage.py runserver
hello
view function.This tutorial has shown you how to create a basic Django routing configuration using the path
method. You can expand on this by adding more view functions and URL patterns to create a more complex web application.
Django URL Patterns 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 path()
Method in URL Patterns:
path()
method in urlpatterns is a simple way to define URL patterns without regular expressions.# 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 Routing:
# 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 urlpatterns path
vs re_path
:
path()
is simple and uses string patterns, while re_path()
allows the use of regular expressions for more complex matching.# 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 Named URL Patterns:
# urls.py urlpatterns = [ path('articles/', views.article_list, name='article-list'), ] # templates <a href="{% url 'article-list' %}">Articles</a>
Django Path Converters in URL Patterns:
# urls.py path('articles/<int:article_id>/', views.article_detail, name='article-detail')
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 include()
Function in URL Patterns:
include()
function allows including other URL patterns from different files to keep the code modular.# urls.py from django.urls import path, include urlpatterns = [ path('articles/', include('articles.urls')), ]
Django URL Parameters in path()
:
path()
to capture dynamic data from the URL and pass it to views.# 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 reverse()
Function in URL Patterns:
reverse()
function allows generating URLs dynamically based on the provided view name and arguments.# 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)