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)
  • 相关阅读:
    c语言面试基础题
    fwrite(&stud[i],sizeof(struct student_type),1,fp)的基本含义
    对于文件操作函数的记录
    将字符串s1复制到字符串s2。
    将键盘输入的几个数据存储到文件里的程序
    利用指针对二维数组进行遍历查找程序
    常见的C语言错误及程序的调试
    文件的基本操作函数及说明
    一个磁盘信息向另一个磁盘信息的复制
    常用流程图符号和基本流程图
  • 原文地址:https://www.cnblogs.com/phoenix13suns/p/3978554.html
Copyright © 2011-2022 走看看