zoukankan      html  css  js  c++  java
  • Bottle python

    1. hello world. URL hander

    import bottle
    
    @bottle.route('/')
    def home_page():
        return "hello world
    "
    
    @bottle.route('/testpage')
    def test_page():
        return "this is a test page"
    
    bottle.debug(True)
    bottle.run(host='localhost',port=8080)

     2. template

    py file

    import bottle
    
    @bottle.route('/')
    def home_page():
        mythings=['apple','banana','peach']
        #option 1
        return bottle.template('hello_world',username='Steve',things=mythings)
        #option 2 use dic
        #return bottle.template('hello_world',{'username':'Steve2','things':mythings})
    
    bottle.debug(True)
    bottle.run(host='localhost',port=8080)

    template file(mix of html and python): hello_world.tpl

    put it in subfold /views

    <!DOCTYPE html>
    <html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        <p>welcome {{username}}
        <p><ul>
            %for thing in things:
            <li>{{thing}}</li>
            %end
        </ul>
    
    </body>
    </html>

     3. POST

    py file

    import bottle
    
    @bottle.route('/')
    def home_page():
        mythings=['apple','banana','peach']
        return bottle.template('hello_world2',username='Steve3',things=mythings)
    
    
    @bottle.post('/fav_fruit')
    def fav_fruit():
        fruit=bottle.request.forms.get('fruit')
        if (fruit==None or fruit==''):
            fruit ='no Fruit'
        return     bottle.template('fruit_select',{'fruit':fruit})
    
    bottle.debug(True)
    bottle.run(host='localhost',port=8080)

    template html

    <!DOCTYPE html>
    <html>
    <head>
        <title>Hello world</title>
    </head>
    <body>
        <p>
            welcome {{username}}
            <p><ul>
                %for thing in things:
                <li>{{thing}}</li>
                %end
            </ul>
            <p>
                <form action="/fav_fruit" method="POST">
                    what is your favourite fruit?
                    <input type="text" name="fruit" size="40" value=""><br>
                        <input type="submit" value="Submit">
                </form>
        </body>
    </html>

     3. cookies, redirect

    import bottle
    
    @bottle.route('/')
    def home_page():
        mythings=['apple','banana','peach']
        return bottle.template('hello_world2',username='Steve3',things=mythings)
    
    
    @bottle.post('/fav_fruit')
    def fav_fruit():
        fruit=bottle.request.forms.get('fruit')
        if (fruit==None or fruit==''):
            fruit ='no Fruit'
        bottle.response.set_cookie("fruit",fruit)
        bottle.redirect("/show_fruit")
    
    
    @bottle.route('/show_fruit')
    def show_fruit():
        fruit = bottle.request.get_cookie("fruit")
        return bottle.template('fruit_select',{"fruit":fruit})
    
    bottle.debug(True)
    bottle.run(host='localhost',port=8080)
  • 相关阅读:
    20169211《Linux内核原理与分析》第四周作业
    20169211《Linux内核原理与分析》第三周作业
    20169211《Linux内核原理与分析》第二周作业
    20169211《Linux内核原理与分析》第一周作业
    20169203《Linux内核原理与分析》第八周作业
    20169203《Linux内核原理与分析》第七周作业
    20169203《Linux内核原理与分析》 第六周作业
    20169203《Linux内核原理与分析》第五周作业
    20169203《Linux内核原理与分析》第四周作业
    20169203《Linux内核原理与分析》第三周作业
  • 原文地址:https://www.cnblogs.com/phoenix13suns/p/3978554.html
Copyright © 2011-2022 走看看