Django路由系统

本文详细介绍了Django框架中的路由系统配置,包括基本格式、参数说明及正则表达式使用方法。通过具体实例展示了如何匹配不同类型的URL路径,并将请求映射到相应的视图函数。

1、路由系统基本格式
urlpatterns = [
path( 要匹配的路径(可以是正则表达式), 视图函数, 参数, 别名)
2、参数说明
(1) 正则表达式:一个正则表达式字符串
(2) 视图函数:一个可调用对象,通常为一个视图函数或一个指定视图函数路径的字符串
(3) •参数:要传递给视图函数的默认参数(字典形式,可选)
(4) •别名:一个可选的name参数
3、正则表达式详解
(1) 在python中使用 re_path模块来写正则表达式
(2) 正则表达式的开始使用“^”表示。
(3) 正则表达式的结束使用“$”表示。
(4) “r” 元字符串 防止正则表达式中的转义。
urls.py

"""day03Django URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from day03Django  import views
from  django.urls  import  path,re_path
urlpatterns = [
    path('admin/', admin.site.urls),
path('laowang1/', views.laowang1),
path('laowang2/', views.laowang2),
path('laowang3/', views.laowang3),
re_path(r'^laowang4/$',views.laowang4),
re_path(r'^$',views.laowang5),#匹配一个空路径  例如:http://127.0.0.1:8000/
re_path(r'^laowang6',views.laowang6),#不设置结尾,可以匹配多个路径 如http://127.0.0.1:8000/laowang6/tianzhu/le
re_path(r'^[a-zA-Z][a-zA-Z][0-9]',views.laowang7),#正则表达式
re_path(r'^laowang8/(\d+)/$',views.laowang8),
re_path(r'^laowang9/(?P<year>[0-9]{4})/$',views.laowang9),#?必须是英文问号
re_path(r'^laowang10/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$',views.laowang10),
re_path(r'^laowang11/(?P<year>[0-9]{4})/$',views.laowang11),#记住写括号
re_path(r'^laowang12/(?P<month>[0-9]{2})/$',views.laowang12,{'year':'2019'}),#记住写括号#在网站内不能修改

re_path(r'^laowang13/$',views.laowang13),#记住写括号
re_path(r'^laowang14/$',views.laowang14,name='lw14'),#记住写括号
re_path(r'^laowang15/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$',views.laowang15,name='lw15'),
#\d表示任意数字,+代表至少初夏一次
#上下移动代码:shift+ctrl+上下键
#ctrl+d复制最后一行或选中的内容
#ctrl+z撤销
]

views.py

from  django.shortcuts import HttpResponse,render
def  laowang1(request):
    return render(request,'laowang1.html',{'name':'天主极乐大帝'})
def  laowang2(request):
    return  render(request,'laowang2.html',{'name':'天主极乐大帝'})
def  laowang3(request):
    return  render(request,'laowang3.html',{'name':'天主极乐大帝'})
def  laowang4(request):
    return  render(request,'laowang4.html',{'name':'天主极乐大帝'})
def  laowang5(request):
    return  render(request,'laowang5.html')
def  laowang6(request):
    return  render(request,'laowang6.html')
def  laowang7(request):
    return  render(request,'laowang7.html')
def  laowang8(request,num):
    """

    :param request:
    :param num:
    :return:
    """
    print(num)
    print(type(num))
    return  render(request,'laowang8.html')
def  laowang9(request,year):
    print(year)
    return  render(request,'laowang9.html')
def  laowang10(request,month,year):
    print(year,month)
    return  render(request,'laowang10.html')
def  laowang11(request,year='2019'):#默认参数必须是字符串类型
    print(year)
    return  render(request,'laowang11.html',{'year':year})
def  laowang12(request,month,year):
    print(year,month)
    return  render(request,'laowang12.html',{'year':year,'month':month})
def  laowang13(request):
    return  render(request,'laowang13.html')
def  laowang14(request):
    return  render(request,'laowang14.html')
def  laowang15(request,month,year):
    print(type(month))
    return  render(request,'laowang15.html',{'year':year,'month':month})

laowang13.html

from  django.shortcuts import HttpResponse,render
def  laowang1(request):
    return render(request,'laowang1.html',{'name':'天主极乐大帝'})
def  laowang2(request):
    return  render(request,'laowang2.html',{'name':'天主极乐大帝'})
def  laowang3(request):
    return  render(request,'laowang3.html',{'name':'天主极乐大帝'})
def  laowang4(request):
    return  render(request,'laowang4.html',{'name':'天主极乐大帝'})
def  laowang5(request):
    return  render(request,'laowang5.html')
def  laowang6(request):
    return  render(request,'laowang6.html')
def  laowang7(request):
    return  render(request,'laowang7.html')
def  laowang8(request,num):
    """

    :param request:
    :param num:
    :return:
    """
    print(num)
    print(type(num))
    return  render(request,'laowang8.html')
def  laowang9(request,year):
    print(year)
    return  render(request,'laowang9.html')
def  laowang10(request,month,year):
    print(year,month)
    return  render(request,'laowang10.html')
def  laowang11(request,year='2019'):#默认参数必须是字符串类型
    print(year)
    return  render(request,'laowang11.html',{'year':year})
def  laowang12(request,month,year):
    print(year,month)
    return  render(request,'laowang12.html',{'year':year,'month':month})
def  laowang13(request):
    return  render(request,'laowang13.html')
def  laowang14(request):
    return  render(request,'laowang14.html')
def  laowang15(request,month,year):
    print(type(month))
    return  render(request,'laowang15.html',{'year':year,'month':month})

laowang14.html

from  django.shortcuts import HttpResponse,render
def  laowang1(request):
    return render(request,'laowang1.html',{'name':'天主极乐大帝'})
def  laowang2(request):
    return  render(request,'laowang2.html',{'name':'天主极乐大帝'})
def  laowang3(request):
    return  render(request,'laowang3.html',{'name':'天主极乐大帝'})
def  laowang4(request):
    return  render(request,'laowang4.html',{'name':'天主极乐大帝'})
def  laowang5(request):
    return  render(request,'laowang5.html')
def  laowang6(request):
    return  render(request,'laowang6.html')
def  laowang7(request):
    return  render(request,'laowang7.html')
def  laowang8(request,num):
    """

    :param request:
    :param num:
    :return:
    """
    print(num)
    print(type(num))
    return  render(request,'laowang8.html')
def  laowang9(request,year):
    print(year)
    return  render(request,'laowang9.html')
def  laowang10(request,month,year):
    print(year,month)
    return  render(request,'laowang10.html')
def  laowang11(request,year='2019'):#默认参数必须是字符串类型
    print(year)
    return  render(request,'laowang11.html',{'year':year})
def  laowang12(request,month,year):
    print(year,month)
    return  render(request,'laowang12.html',{'year':year,'month':month})
def  laowang13(request):
    return  render(request,'laowang13.html')
def  laowang14(request):
    return  render(request,'laowang14.html')
def  laowang15(request,month,year):
    print(type(month))
    return  render(request,'laowang15.html',{'year':year,'month':month})

laowang15.html

<!DOCTYPE html>
{# 加载static标签 #}
{#{% load  static %}#}
{#{% load  static %}{# load负载,装载量 #}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

老王15-->
{{ year }}-->{{ month }}
<a  href="/laowang13/">调转到13</a>

</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值