官方中文文档地址:http://www.pythondoc.com/flask-sqlalchemy/quickstart.html
文件结构
app.py
from flask import Flask, render_template
from flask import url_for
from flask import request
from flask import session
from flask import abort,redirect
from flask import make_response
from flask import json
from flask import jsonify
import os
app = Flask(__name__)
# 访问模板,会默认在templates文件夹下找文件
# 路由默认只支持GET,如有需要需另外增加
@app.route('/')
def hello_world():
return "Hello!"
# 在/test001界面显示test001
@app.route('/test001')
def test001():
return "test001"
# 含参路由的写法<>表示要传递的参数
# 其中可设置参数类型,默认文字符串型
@app.route('/test002/<int:name>')
def test002(name):
return "Name is %s " % name
# 调用jinja2模板,并向其传递参数
@app.route('/test003')
def test003():
# 为静态文件的访问生成url链接
url_for('static', filename='1.txt')
return render_template('index.html',
name="xsb",
list=[1, 2, 3, 4, 5],
dd={'a': 1, 'b': 2}
)
@app.route('/test004', methods=['GET', 'POST'])
def test004():
res=''
if request.method == 'GET':
res=res+'GET'
elif request.method == 'POST':
res=res+'POST'
# 获取表单提交的数据
res = res + ' username :' + request.args.get(key='username')
# res = res+' username :'+request.form['username'] # 访问表单数据
return res
# 文件上传页面的访问
@app.route('/test005_1')
def test005_1():
return render_template('upload.html')
# 简单上传文件样例
@app.route('/test005_2', methods=['POST'])
def test005_2():
if request.method == 'POST':
f = request.files['fileUpload']
# 注意,这里不能信任用户给的文件名,尽量自己生成一个文件名,实在不行的话可以用Werkzeug提供的secure_filename() 函数
f.save('upload/1.txt')
return "上传成功"
# cookie的设置
@app.route('/test006_1')
def test006_1():
resp = make_response("lalalalala")
resp.set_cookie('username', 'xsb')
return resp
# cookie的获取
@app.route('/test006_2')
def test006_2():
res = request.cookies.get('username')
return res
# 重定向样例
@app.route('/test007_1')
def test007_1():
return redirect(url_for('test007_2'))
# 401静止访问报错样例
@app.route('/test007_2')
def test007_2():
abort(401)
# 设置所有404错误的处理方式
@app.errorhandler(404)
def test007_3():
return render_template('sample404.html'), 404
# 设置秘钥
app.config['SECRET_KEY'] = os.urandom(24)
# app.config['SECRET_KEY'] = '123456'
# 登录界面
@app.route('/test008_1')
def test008_1():
return render_template('login.html')
# 设置会话
@app.route('/test008_2', methods=['GET', 'POST'])
def test008_2():
session['username'] = request.form['username']
return redirect(url_for('test008_4'))
# 删除会话
@app.route('/test008_3')
def test008_3():
session.pop(key='username')
return redirect(url_for('test008_4'))
# 展示会话内容
@app.route('/test008_4')
def test008_4():
return "username: " + str(session.get('username'))
@app.route('/test009')
def test009():
return jsonify(name='xsb', ge='20')
if __name__ == '__main__':
app.run()
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<h1>Hello!</h1>
<br/>
{# 要显示的参数可以用双括号表示 #}
{{ name }} <br>
{# 传列表 #}
{{ list }} <br>
{{ list.2 }} <br>
{{ list[2] }}<br>
{# 传字典 #}
{{ dd }}<br>
{{ dd.a }}<br>
{{ dd['a'] }}<br>
<hr>
{# 控制语法基础 #}
{% for my in list %}
{% if my > 2 %}
{{ my }} <br>
{% endif %}
{% endfor %}
</body>
</html>
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/test008_2" method="post">
<p><input type=text name=username>
<p><input type=submit value=Login>
</form>
</body>
</html>
sample404.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
您访问的页面不存在lalalalalalalalala
</body>
</html>
upload.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>简单上传文件样例</title>
</head>
<body>
<form action="/test005_2" method="post" enctype="multipart/form-data">
<input type="file" name="fileUpload" />
<input type="submit" value="上传文件" />
</form>
</body>
</html>