003 Django 路由

路由

路由配置 - path

  • 导入
    • from django.urls import path
  • 语法
    • path(route, views, name=None)
  • 参数
    • route: 字符串类型,匹配的请求路径
    • views: 指定路径所对应的视图处理函数的名称
    • name: 为地址起别名,在模板中地址反向解析的时候使用,因为有的时候我们的路径很长,我们就需要用别名来替代他们

转换器

有的时候我们需要输出路径不变但是数量改变的页面,这个时候大量创建路由就显得很难

image-20220621211619198

  • 语法: <转换器类型:自定义名>

  • 作用: 若转换器类型匹配到对应类型的数据,则将数据按照关键字传参的方式传递给视图函数

  • 例子: path(‘page/<int:page>’,views.xxx)

类型

image-20220621212507699

创建一个转换器页面

# urls.py 文件内
from django.urls import path
from . import views

urlpatterns = [
    # 转换器
    path('a/<int:page>',views.show_numbers_view)
]

# views.py 文件内
from django.http import HttpResponse

def show_numbers_view(request,page):
    html = f'这是编号{page}'
    return HttpResponse(html)

此时我们只需要在网站上面访问相应的数量就可以访问到对应的网站

e9d6b110-f20e-4d5a-93b4-325d0164cff0

image-20220621213844049

# urls.py 文件内
from django.urls import path
from . import views

urlpatterns = [
    path('<int:num1>/<str:op>/<int:num2>',views.calculate_view)
]
# views.py 文件内
from django.http import HttpResponse

def calculate_view(request,num1,num2,op):
    html = "当前结果为:"
    
    if op not in ['add','sub','mul']:
        return HttpResponse("你输入的运算符不正确")
    
    if op == 'add':
        html += str(num1 + num2)
        return HttpResponse(html)
    
    elif op == 'sub':
        html += str(num1 - num2)
        return HttpResponse(html)
    
    elif op == 'mul':
        html += str(num1 * num2)
        return HttpResponse(html)

77661861-281d-4b5f-a99e-d0948e921670

26b14a81-f2cb-48b6-a945-e7de8b107303

正则匹配路由 - re_path()

  • 在 url 的匹配过程中可以使用正则表达式进行精确匹配
  • 这个库并没有直接导入,这个re_path 与 path 是同一级的,我们需要使用 import 导入
  • 语法:
    • re_path(reg, view, name=xxx)
    • 正则表达式为命名分组模式(?P<name>pattern);匹配提取参数后用关键字传参的方式传递给视图函数

使用 re_path() 修改我们的网页计算机

我们现在希望给我们的网页计算机添加一个限制条件,那就是我们只做两位数的加法,如果这个数字超过了两位我们就不会对他们进行运算

这个时候我们使用 path() 就无法满足我们的需求,我们就需要使用 re_path()

# urls.py 文件内
from django.urls import path, re_path
from . import views

urlpatterns = [
    # 其中<num1>表示变量名,\d{1,2}表示匹配数字一次或者两次
    re_path(r'^(?P<num1>\d{1,2})/(?P<op>\w+)/(?P<num2>\d{1,2})$',views.calculate2_view),
    path('<int:num1>/<str:op>/<int:num2>',views.calculate_view)
]
# views.py 内
from django.http import HttpResponse

# 普通的数字加减
def calculate_view(request,num1,num2,op):
    html = "当前使用 path 结果为:"
    
    if op not in ['add','sub','mul']:
        return HttpResponse("你输入的运算符不正确")
    
    if op == 'add':
        html += str(num1 + num2)
        return HttpResponse(html)
    
    elif op == 'sub':
        html += str(num1 - num2)
        return HttpResponse(html)
    
    elif op == 'mul':
        html += str(num1 * num2)
        return HttpResponse(html)

 # 使用正则表达式的数字加减
def calculate2_view(request,num1,num2,op):
    html = "当前使用了 re_path 结果为:"
    
    num1 = int(num1)
    num2 = int(num2)
    
    if op not in ['add','sub','mul']:
        return HttpResponse("你输入的运算符不正确")
    
    if op == 'add':
        html += str(num1 + num2)
        return HttpResponse(html)
    
    elif op == 'sub':
        html += str(num1 - num2)
        return HttpResponse(html)
    
    elif op == 'mul':
        html += str(num1 * num2)
        return HttpResponse(html)

5893d60f-d608-49e3-9c5c-9ce5e8ecdf3d

小练习 - 使用正则匹配出生日期

我们需要让网页能够识别我们的出生日期,而且正着能够识别,反转也要能够识别

  • 例如 2002/8/24 是正常的
  • 24/8/2002 同样也需要能够识别出来
# urls.py 文件内
from django.urls import path, re_path
from . import views

urlpatterns = [
    # 正匹配
    re_path(r'^(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})$',views.show_birthday),
    # 反向匹配
    re_path(r'^(?P<day>\d{1,2})/(?P<month>\d{1,2})/(?P<year>\d{4})$',views.show_birthday),  
    # 如果匹配格式出现错误就会进入下面这一条路由
    path('<int:num1>/<str:op>/<int:num2>',views.calculate_view), 
]
# views.py 文件内
from django.http import HttpResponse

def show_birthday(request,year,month,day):
    return HttpResponse(f'您的生日是{year}年-{month}月-{day}日')

def calculate_view(request,num1,num2,op):
    html = "当前使用 path 结果为:"
    
    if op not in ['add','sub','mul']:
        return HttpResponse("你输入的运算符不正确")
    
    if op == 'add':
        html += str(num1 + num2)
        return HttpResponse(html)
    
    elif op == 'sub':
        html += str(num1 - num2)
        return HttpResponse(html)
    
    elif op == 'mul':
        html += str(num1 * num2)
        return HttpResponse(html)

e1bc262a-d727-4961-9ac1-367da0110715

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值