路由控制器:
from django.urls import path
from django.urls import re_path
基本使用:
from django.contrib import admin
from django.urls import path,re_path
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
path(r'^articles/2003/$', views.special_case_2003),
re_path(r'^articles/([0-9]{4})/$', views.year_archive),
re_path(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive),
re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive2),
]
路由分发:
- 使用路由分发(include),让每个app目录都单独拥有自己的 urls
全局urls配置
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('app01/',include("app01.urls"))
]
app01.urls配置
from django.urls import path
from app01 import views
urlpatterns = [
path('',views.index)
]
views.index配置
from django.shortcuts import render
def index(request):
return render(request,"index.html")
index.html配置
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>hello index.........</h3>
</body>
</html>
执行结果:

路由转发器:
- 内置的url转换器并不能满足我们的需求,因此django给我们提供了一个接口可以让我们自己定义自己的url转换器
全局urls配置
from django.contrib import admin
from django.urls import path,register_converter,include
from app01.views import index
class Mobile(object):
regex="1[3-9]\d{9}"
def to_python(self,values):
return values
register_converter(Mobile, "mo")
urlpatterns = [
path("index/<mo:m>",index)
]
app01.views函数index配置
from django.shortcuts import HttpResponse
def index(request,m):
print(":::",type(m))
return HttpResponse(f"hi,{m}用户")
执行结果:

反向解析:
app07/urls.py配置文件
from django.urls import path
from app07.views import order,index
urlpatterns = [
path('index/', index, name="ind"),
path('order/', order, name="ord")
]
模板反向解析:
app07/views.py配置文件
from django.shortcuts import render
def order(request):
return render(request,"order.html")
def index(request):
return render(request,"index.html")
templates/order.html配置文件
{% extends 'basis.html' %}
{% block content %}
{
{{ block.super }}
<h3>修改原有数据</h3>
{% endblock %}
templates/basis.html配置文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% block content %}
<h1>1111111111111111111</h1>
{% endblock %}
{% block mes %}
<h1>22222222222222222</h1>
{% endblock %}
<p><a href="{% url 'ind' %}">跳转到主页</a></p>
<style>
.advertise{
width: 200px;
height: 100px;
background-color: orange;
position: fixed;
bottom: 10px;
right: 10px;
}
</style>
<div class="advertise">广告</div>
</body>
视图反向解析:
app07/views.py配置文件
from django.shortcuts import render,redirect
from django.urls import reverse
def order(request):
print("反向解析地址:",reverse("ord"))
return redirect(reverse("ind"))
def index(request):
return render(request,"index.html")
templates/index.html配置文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<H2>首页页面</H2>
</body>
</html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了