flask web

 

 

 

url_for()

@app.route('/hello/')
def hello_world():
    return 'heeee'


@app.route('/user/<name>')
def hello_user(name):
    if name == 'admin':
        return redirect(url_for('hello_world'))
    else:
        return 'abc'    

 

 

静态文件

存放在static文件夹下

 http://127.0.0.1:5000/static/a.html

 

模板

 

 

@app.route('/hello/')
def hello_world():
    return render_template('index.html')

 

处理表单

@app.route('/login', methods=['POST', 'GET'])
def login():
    error = None
    if request.method == 'POST':
        if valid_login(request.form['username'],
                       request.form['password']):
            return log_the_user_in(request.form['username'])
        else:
            error = 'Invalid username/password'
    # the code below is executed if the request method
    # was GET or the credentials were invalid
    return render_template('login.html', error=error)

 

文件上传

 

Cookies

读取

@app.route('/a')
def index():
    username = request.cookies.get('username')
    return username

 

 

存储

 

会话

 

 

错误页

@app.errorhandler(404)
def page_not_found(error):
    return render_template('error.html'), 404

 

 

 

json解析

 导入

from flask import Flask,jsonify

1.列表

def index():
    arr=['mkdir','md','touch']
    return jsonify(arr)

2.字典

@app.route('/')
def index():
    phonebook = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
    return jsonify(phonebook)

3.嵌套

@app.route('/')
def index():
    phonebook = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
    return jsonify({'code':'0','msg':'OK','phonebook':phonebook})

 

posted @ 2019-07-06 15:52  富坚老贼  阅读(334)  评论(0编辑  收藏  举报