zoukankan      html  css  js  c++  java
  • bottle route regular expression

    Routing

    As you have learned before, routes are used to map URLs to callback functions. These functions are executed on every request that matches the route and their return value is returned to the browser. You can add any number of routes to a callback using the route() decorator.

    <span class="kn">from</span> <span class="nn">bottle</span> <span class="kn">import</span> <span class="n">route</span>
    
    <span class="nd">@route</span><span class="p">(</span><span class="s">'/'</span><span class="p">)</span>
    <span class="nd">@route</span><span class="p">(</span><span class="s">'/index.html'</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">index</span><span class="p">():</span>
        <span class="k">return</span> <span class="s">"<a href='/hello'>Go to Hello World page</a>"</span>
    
    <span class="nd">@route</span><span class="p">(</span><span class="s">'/hello'</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">hello</span><span class="p">():</span>
        <span class="k">return</span> <span class="s">"Hello World!"</span>
    

    As you can see, URLs and routes have nothing to do with actual files on the web server. Routes are unique names for your callbacks, nothing more and nothing less. All URLs not covered by a route are answered with a “404 Page not found” error page.

     

    Dynamic Routes

    Bottle has a special syntax to add wildcards to a route and allow a single route to match a wide range of URLs. These dynamic routes are often used by blogs or wikis to create nice looking and meaningful URLs such as /archive/2010/04/21 or /wiki/Page_Title. Why? Because cool URIs don’t change. Let’s add a :name wildcard to our last example:

    <span class="nd">@route</span><span class="p">(</span><span class="s">'/hello/:name'</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">hello</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">"Hello </span><span class="si">%s</span><span class="s">!"</span> <span class="o">%</span> <span class="n">name</span>
    

    This dynamic route will match /hello/alice as well as /hello/bob. Each URL fragment covered by a wildcard is passed to the callback function as a keyword argument so you can use the information in your application.

    Normal wildcards match everything up to the next slash. You can add a regular expression to change that:

    <span class="nd">@route</span><span class="p">(</span><span class="s">'/object/:id#[0-9]+#'</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">view_object</span><span class="p">(</span><span class="nb">id</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">"Object ID: </span><span class="si">%d</span><span class="s">"</span> <span class="o">%</span> <span class="nb">int</span><span class="p">(</span><span class="nb">id</span><span class="p">)</span>
    

    As you can see, the keyword argument contains a string even if the wildcard is configured to only match digits. You have to explicitly cast it into an integer if you need to.

    reference bottle doc

    example:

    @route("/news/:filename#p\d+and[\s\S]*?\.html#")

    @view("article/detail")

    def article_detail(filename):

        print filename

        match = re.search(r"^p(\d+)and[\s\S]*?\.html$", filename)

        id=None

        if match:

            id=int(match.group(1))

            print "id=%s"%id

            a=Article.get(id)

            if a is not None:

                b=Article.query.filter_by(id < 20).one()

                if b is None:

                    print "b None"

                else:

                    print "b not None"

                return {"article":a}

            else:

                return {}

        return {}

  • 相关阅读:
    【解决】Ubuntu命令行弹出光驱的方法
    【8086汇编基础】02寻址方式和MOV指令
    【8086汇编基础】03变量、数组和常量的处理
    Ubuntu12.10 内核源码外编译 linux模块编译驱动模块的基本方法
    【8086汇编基础】05常用函数库文件emu8086.inc
    Ubuntu12.10 使用DNW传数据 进行ARM开发板烧写
    【8086汇编基础】04中断
    【8086汇编基础】01汇编语言简要介绍
    飞凌OK6410开发板移植uboot官方最新版uboot2012.10.tar.bz2
    【8086汇编基础】00基础知识各种进制的数据
  • 原文地址:https://www.cnblogs.com/lexus/p/1817077.html
Copyright © 2011-2022 走看看