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'}
  • 相关阅读:
    Qt5对付中文真好用
    Qt5下的常见问题————C1083
    macbook pro retina 编程字体推荐
    boost::xml————又一次失败的尝试
    boost::xml——基本操作以及中文乱码解决方案 (续)
    C++单元测试2
    C++单元测试
    生成dll文件的示例
    咋一看你能看明白吗?
    boost::function实践——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》
  • 原文地址:https://www.cnblogs.com/Zzbj/p/11747525.html
Copyright © 2011-2022 走看看