ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

003 Django 路由

2022-07-10 19:35:53  阅读:163  来源: 互联网

标签:return views Django 003 html HttpResponse path 路由 op


路由

目录

路由配置 - 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

标签:return,views,Django,003,html,HttpResponse,path,路由,op
来源: https://www.cnblogs.com/BEMAKE/p/16463802.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有