bottle文档bottle文档1Jinja2 模板用法
1.基本框架
debug : 显示错误
 reloader:代码更新后自动加载新代码
from bottle import route, run, jinja2_template
@route('/')
def index():
return jinja2_template('templates/index.html')
run(server='tornado', host='127.0.0.1', port='8082', reloader=True)
from bottle import Bottle, run, route
web = Bottle()
@web.route('/')
def index():
return 'I am bottle!'
run(web) # 默认端口为8080
2.URL装饰器
@route('/')
@route('/', method=['GET', 'POST'])
if request.method == 'POST':
  return '这是一个post请求'动态路由基础:将请求参数放入URL路径中
直接获取字符串参数:
URL形式1:/user/
 URL形式2:/user//
 访问URL: /user/tom
http://127.0.0.1/index/tom
@route('/index/<name>')
def index(name):
    print(name)
    return 'I am bottle!'
run(host='127.0.0.1', port=80, debug=True, reloader=True)3. 返回静态文件

强制下载:download=True
 重命名下载:download=‘6.txt’
http://127.0.0.1/index/root.txt
@route('/index/<filename>')
def index(filename):
    print(filename)
    return static_file(filename, root='', download='6.txt')
run(host='127.0.0.1', port=80, debug=True, reloader=True)4. URL转向
from bottle import run, route, error, abort, redirect
@route('/zhuanxiang')
def index():
return redirect('/')
@route('/about')
def index():
abort(404, 'err 404') # 转向404页面
5. 404 错误页面
from bottle import run, route, error
@error(404)
def err(err):
return '亲,您要的页面丢失了!!!'
6. 获取GET、POST请求的参数
(1)GET 参数提供方法
在URL 链接最后添加“?名称=值&名称=值…”,可手工输入提供、也可通过超链接提供或表单形式。
 例如:http://127.0.0.1/?key=520&username=1314&sign=666
2.获取提交的参数
提交数据查看 jquery 提交数据 ajax 
nickname = request.POST.getunicode('money')
print(money)3.返回json格式
return json.dumps({'data': 403})登录
@route('/login', method=['GET','POST'])
def login(db):
    if request.method == 'GET':
        if request.get_cookie('SchoolShop'):
            userinfo = request.get_cookie('SchoolShop', secret='safe')
            info = json.loads(userinfo)
            uname = info['username']
            psd = info['password']
            return redirect('/my')
    if request.method == 'POST':
        username = request.POST.getunicode('username')
        psd = request.POST.getunicode('password')
        print('{}  {}',format(username, psd))
        if (username == '' and psd == ''):
            return json.dumps({'data': 401})  # 请求需要对用户身份进行认证
        # 从数据库 查询username的密码
        dbuser_psd = db.query(Users).filter_by(username=username).first().password
        print("账号为:" + username)
        print("db密码为:" + dbuser_psd + "  输入密码:" + psd)
        # 判断密码是否正确
        if dbuser_psd == psd:
            try:
                # 查询用户昵称
                nickname = db.query(Users).filter_by(username=username).first().nickname
                print("用户昵称:" + nickname)
                session_val = json.dumps({'username': username, 'password': psd}).encode('utf-8')
                # 返回cookie
                response.set_cookie("SchoolShop", session_val, secret='safe', max_age=80000)
                # 返回登录成功
                return json.dumps({'data': 200})
            except:
                return json.dumps({'data': 403})
        else:
            # 账号或密码错误
            return json.dumps({'data': 403})
    return jinja2_template('templates/login.html')验证是否登录 使用cookie
@route('/addressAdd', method=['GET','POST'])
def addressAdd(db):
    is_login = 0
    uname = ''
    try:
        if request.get_cookie('SchoolShop'):
            userinfo = request.get_cookie('SchoolShop', secret='safe')
            info = json.loads(userinfo)
            uname = info['username']
            psd = info['password']
            try:
                # username = db.query(Users).filter_by(username=uname).first().username
                password = db.query(Users).filter_by(username=uname).first().password
                if (password == psd):
                    is_login = 1
                else:
                    # print('cookie失效')
                    return redirect('/')
            except:
                # print('数据库查询错误')
                return redirect('/')
        else:
            return redirect('/login')
        if (is_login == 1):
            if request.method == 'GET':
                return jinja2_template('templates/addressAdd.html')
            # 提交用户新增的地址
            if request.method == 'POST':
                receiver = request.POST.getunicode('receiver')
                phone = request.POST.getunicode('phone')
                print('{} {} {}'.format(receiver, phone, pos))
    except:
        return redirect('/login')
    return redirect('/')                










