zoukankan      html  css  js  c++  java
  • python之routes入门

    一、入门

    from routes import Mapper
    
    map = Mapper()  # 创建一个mapper()路由实例对象
    
    # connect注册路由信息
    # 路由名称'zbj', 路径是 '/clj', controller为 'main', action为 'index'
    # 匹配到此条路由URL的请求:交由controller类处理,请求预调用的函数index
    map.connect('zbj', '/clj', controller='main', action='index')
    
    # 创建好路由条目后,即可进行匹配,调用match方法,匹配路径 /clj
    result = map.match('/clj')
    
    # 输出匹配结果。匹配上之后,匹配结果是一个字典,保存的是后续调用的类和类的方法。
    # 如果匹配不上的话,就会输出 None。
    print(result)  # {'controller': 'main', 'action': 'index'}

    二、无名路由

    from routes import Mapper
    
    map = Mapper()
    
    # 注册一个无名路由,action可以从匹配路由中获得
    map.connect('/home/{action}/{id}', controller='home')
    result = map.match('/home/index/200')  #
    print(result)  # {'action': 'index', 'id': '200', 'controller': 'home'}
    
    # { }用来指定里面匹配的字段是什么, : 表示的是匹配字段的格式
    map.connect('/home/{action:index|jia}/{id:d+}', controller='home')
    res = map.match('/home/jia/200')
    print(res)  # {'action': 'jia', 'id': '200', 'controller': 'home'}

    三、conditions限制

    from routes import Mapper
    
    map = Mapper()
    
    # 只匹配GET、HEAD请求。
    map.connect('/user/list', controller='user', action='list',
                conditions={'method': ['GET', 'HEAD']})
    
    result = map.match('/user/list')
    print(result)  # {'action': 'list', 'controller': 'user'}
  • 相关阅读:
    js获取input file文件二进制码
    nginx新手入门
    代码神器Atom,最常用的几大插件,你值得拥有。
    css3 3d 与案例分析
    express搭建简易web的服务器
    hosts文件管理和nginx总结
    css3 3D
    问题大神
    面试题整理
    版本控制简介,git使用----使用GitHub托管代码
  • 原文地址:https://www.cnblogs.com/Zzbj/p/11747525.html
Copyright © 2011-2022 走看看