Django基础2——URL路由系统

一、基本了解

概念:

  • 路由系统就是URL路径和视图函数的一个对应关系,也可以称为转发器。

1.文件urls.py编写语法。

##定义文件devops/urls.py

urlpatterns = [
    path(regex, view, kwargs=None, name=None)
]
参数 释义
urlpatterns 一个列表,每一个path()函数是一个元素,对应一个视图
regex 一个字符串或者正则表达式,匹配URL
view 对应一个函数视图或者类视图(as_view()的结果),必须返回一个HttpResponse对象,Django将这个对象转换成一个HTTP响应
kwargs 可选,字典形式数据传递给对应视图
name 可选,URL名称

2.文件使用方法种类。
在这里插入图片描述

二、url路由分发

优点:

  • urls配置解耦,方便管理

原理:

  1. 浏览器访问项目根urls.py文件中的路由规则,通过其中定义的路由规则转发到具体的子urls.py文件中。
  2. 子urls.py文件再调用对应的函数,最终返回一个应用效果。
    在这里插入图片描述

1.定义根路由规则,文件为devops/urls.py。

from django.contrib import admin
from django.urls import path,include
from devops import views

urlpatterns = [
    path('index/', views.index),
    path('admin/', admin.site.urls),
    path('logs/', views.logs),
    path('apm/', include('apm.urls')),   ##访问http://xxx/apm/qingjun
    path('ivpm/', include('ivpm.urls'))   ##访问http://xxx/ivpm/baimu
]

2.定义子路由规则。

##第一个文件apm/urls.py
from django.urls import path
from apm import views

urlpatterns = [
    path('qingjun/', views.wuhan),
]


##第二个文件apm/urls.py
from django.urls import path
from ivpm import views

urlpatterns = [
    path('baimu/', views.beijing),
]

3.定义视图函数。

##第一个路由规则指定的视图函数,文件为apm/views.py。
from django.shortcuts import render,HttpResponse

def wuhan(request):
    return HttpResponse("这是第一个功能首页!!")


##第二个路由规则指定的视图函数,文件为ivpm/views.py。
from django.shortcuts import render,HttpResponse

def beijing(request):
    return HttpResponse("这是第二个功能首页!!")

4.访问网页,验证效果。
在这里插入图片描述

三、正则匹配

  • URL路径可以使用正则表达式匹配,re_path()替代path()。

1.正常匹配效果,需要在输入uri。

##url路由系统。
from django.contrib import admin
from django.urls import path,include,re_path    ##导入re_path模块。
from devops import views

urlpatterns = [
    path('index/', views.index),   ##正常匹配。
    path('admin/', admin.site.urls),
]


##函数视图。
from django.shortcuts import render,HttpResponse
def index(request):
    return HttpResponse("首页")

在这里插入图片描述

2.使用正则匹配首页,不需要输入uri示例。

##url路由系统。
from django.contrib import admin
from django.urls import path,include,re_path    ##导入re_path模块。
from devops import views

urlpatterns = [
    # path('index/', views.index),   ##正常匹配。
    re_path('^$', views.index),      ##使用正则匹配。
    path('admin/', admin.site.urls),
]


##函数视图。
from django.shortcuts import render,HttpResponse
def index(request):
    return HttpResponse("首页")

在这里插入图片描述

四、压缩归档超链接

  • 实现在网页上点击一个超链接,会跳转到第二个网页,以此类推。

1.实现网站首页显示超链接。

##############################################################
1、视图文件:devops/views.py
from django.shortcuts import render,HttpResponse
def index(request):
    return render(request,'index.html')

##############################################################
2、根路由系统文件:devops/urls.py
from django.contrib import admin
from django.urls import path,re_path
from devops import views

urlpatterns = [
    re_path('^$', views.index),
    path('admin/', admin.site.urls),
    re_path('^articles/([0-9]{4})/$', apm_views.year_articles),
    re_path('^articles/([0-9]{4})/([0-9]{2})/$', apm_views.month_articles),
    re_path('^articles/([0-9]{4})/([0-9]{2})/([0-9]+)$', apm_views.id_articles)
]

##############################################################
3、编写步骤2中转发的apm_views视图函数,apm/views.py文件。
def year_articles(request,year):
    return HttpResponse("这里是%s年的文章" %year)

def month_articles(request,year,month):
    return HttpResponse("这里是%s年%s月的文章" %(year,month))

def id_articles(request,year,month,id):
    return HttpResponse("这里是%s年%s月的文章id%s" %(year,month,id))

##############################################################
4、创建template/index.html模板文件。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>网站首页</title>
</head>
<body>
<h1>博客文章归档</h1>
<a href="http://127.0.0.1:8000/articles/2020/">2020年的文章(70)</a><br>
<a href="http://127.0.0.1:8000/articles/2020/10">2020年10月的文章(13)</a>
</body>
</html>

##释义。
<a href=“超连接地址">自定义显示文字</a>   
##<br>表示转义换行。

在这里插入图片描述

2.验证超链接效果。点击”2020年的文章(70)“会跳转到如下页面。
在这里插入图片描述
3.验证归档效果。
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

优化一:使用分组名称功能

1.不使用分组效果时,需要根据视图函数中的位置变量进行传参,位置一对一,不然就会显示错乱。

##如下是视图函数文件views.py内容,year、month、id参数是从根urls.py文件中的分组传来的,第一个分组传year,第二个分组传到month,第三个分组传到id。

def id_articles(request,year,month,id):
    return HttpResponse("这里是%s年%s月的文章id%s" %(year,month,id))


##############################################################
##根urls.py文件。
urlpatterns = [
    ......
    re_path('^articles/([0-9]{4})/([0-9]{2})/([0-9]+)$', apm_views.id_articles)
]

在这里插入图片描述

2.使用分组名称就可以解决这个问题,视图函数中的参数位置不再受到限制,可以随意调换位置。

##根urls.py文件,使用分组名称。
urlpatterns = [
    ......
    re_path('^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<id>[0-9]+)$', apm_views.id_articles)
]

##############################################################
##调换位置参数id和month位置。
def id_articles(request,year,id,month):
    return HttpResponse("这里是%s年%s月的文章id%s" %(year,month,id))

在这里插入图片描述

优化二:使用url名称功能

  • 在前端代码里经常会指定URL,例如超链接,提交表单等,倘若超链接地址发生改变,使用上文的那种硬编码写法就会报错,此时就需要批量修改超链接的新地址,这时用URL反查就方便多了。
示例 语法
之前 <a href=“/hello”>你好
之后 <a href=“{% url ‘hello’ %}”>你好

4.1 使用功能之前效果展示

1.代码如下。

##############################################################
1、根路由规则文件urls.py。
from django.contrib import admin
from django.urls import path,include,re_path
from devops import views
from apm import views as apm_views

urlpatterns = [
    re_path('^$', views.index),
    path('admin/', admin.site.urls),
    path('logs/', views.logs),
    path('apm/', include('apm.urls')),
    path('ivpm/', include('ivpm.urls')),       ##看这行,效果实现转发url到ivpm.urls文件。
    re_path('^articles/([0-9]{4})/$', apm_views.year_articles),
    re_path('^articles/([0-9]{4})/([0-9]{2})/$', apm_views.month_articles),
    re_path('^articles/([0-9]{4})/([0-9]{2})/([0-9]+)$', apm_views.id_articles)
]

##############################################################
2、子路由规则文件ivpm/urls.py文件。
urlpatterns = [
    path('baimu/', views.beijing),     ##转发到ivpm/views文件中的beijing函数。
]

##############################################################
3、子路由系统转到到视图函数。
from django.shortcuts import render,HttpResponse

def beijing(request):
    return HttpResponse("这是第二个功能首页!!")

##############################################################
4、模板文件template/index.html。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>网站首页</title>
</head>
<body>
<h1>博客文章归档</h1>
<a href="http://127.0.0.1:8000/articles/2020/">2020年的文章(70)</a><br>
<a href="http://127.0.0.1:8000/articles/2020/10">2020年10月的文章(13)</a><br>
<a href="http://127.0.0.1:8000/ivpm/baimu">返回到第2个模块首页</a>
##<a href="ivpm/baimu">返回到第2个模块首页</a>   ##与上面一行效果一样,使用相对路径。
</body>
</html>

2.正常访问子路由规则文件ivpm/urls.py文件中定义的访问路径。
在这里插入图片描述
3.当子路由规则文件ivpm/urls.py文件中定义的访问路径更新后,那原来的代码就不能正常访问指定页面了。
在这里插入图片描述
4.可以通过更改代码恢复原来的访问效果。如此可见很是麻烦,所以常常给url定义名称,对其名称进行引用。
在这里插入图片描述

4.2 使用功能之后效果展示

1.修改子路由规则文件ivpm/urls.py文件,对url进行命名。

urlpatterns = [
    path('baimu33/', views.beijing,name='baimu'),
]

2.修改模板文件template/index.html,引用url名称,这样依赖后面的超链接地址被改变会自动转发到正确地址。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>网站首页</title>
</head>
<body>
<h1>博客文章归档</h1>
<a href="http://127.0.0.1:8000/articles/2020/">2020年的文章(70)</a><br>
<a href="http://127.0.0.1:8000/articles/2020/10">2020年10月的文章(13)</a><br>
<a href="{% url 'baimu' %}">返回到第2个模块首页</a>    ##固定语法,baimu表示url名称。
</body>
</html>

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/yi_qingjun/article/details/132421862