导入对应模块
from flask import Flask from flask import render_template from flask import request from flask import url_for, redirect from mysql import DbMysql app = Flask(__name__)
模版渲染&数据传递
# 直接输出页面 @app.route('/') def index(): data_name = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] data_list = [150, 230, 224, 218, 135, 147, 260] return render_template('index.html', data_name=data_name, data_list=data_list)
对接MySql数据库
@app.route('/echarts') def get_echarts(): db = DbMysql() items = db.get_items() return render_template('echarts.html', items=items)
get路径参数
# get路径参数 @app.route('/api') def get_api(): # api?user=Flask&time=2021-09-15&page=7&pagesize=100 print(request.path) print(request.full_path) # return request.args.get('user') # 获取某个指定的参数 return request.args.__str__()
动态url参数
@app.route('/<lang_code>/index') def get_lang(lang_code): return lang_code
跳转页面
# 跳转页面; @app.route('/test') def test(): print('this is test1') return redirect(url_for('get_echarts')) # with app.test_request_context(): # print(url_for('index')) # print(url_for('get_test')) # print(url_for('get_echarts')) # print(url_for('user', name='lockdatav')) # print(url_for('page', num=1, q='hadoop mapreduce 10%3')) # print(url_for('static', filename='uploads/01.jpg')) if __name__ == '__main__': app.run(host='localhost', port=8800, debug=True)
lockdatav Done!