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 {}

  • 相关阅读:
    架构师之路--视频业务介绍,离线服务架构和各种集群原理
    架构师之路--怎样聊技术天,限流技术和各类编程语言
    一条项目中常用的linux命令引发的经典算法题
    架构师之路--应用架构的选型和dubbo
    乐视开放平台技术架构-servlet和spring mvc篇
    架构师之路--从业务角度谈缓存的选型
    架构师之路--谈业务的合理架构
    IO回忆录之怎样过目不忘(BIO/NIO/AIO/Netty)
    520特篇-爱的境界
    编程十年的十种武学境界
  • 原文地址:https://www.cnblogs.com/lexus/p/1817077.html
Copyright © 2011-2022 走看看