1.创建一个小的flask应用
1 2 3 4 5 6 7 8 9 10
| (文件名为app.py) from flask import Flask app = Flask(__name__)
@app.route('/') def hello_world(): return 'Hello World!'
if __name__ == '__main__': app.run()
|
上述代码便是一个最简单的flask应用,使用python app.py
即可运行,命令行运行情况如下
1 2
| $ python app.py * Running on http://127.0.0.1:5000/
|
2.flask的路由
###(1)下面的代码表示在http://127.0.0.1:5000/hello路径下可以在网页中看到返回值`hello`
1 2 3 4 5 6
| from flask import Flask app = Flask(__name__)
@app.route('/hello') def hello(id): return("hello")
|
###(2)下面的代码表示可以在获取路由中的值,如在路径中输入路径http://127.0.0.1:5000/hello/123,页面中将会显示返回值`hello 123`
1 2 3 4 5 6
| from flask import Flask app = Flask(__name__)
@app.route('/hello/<id>') def hello(id): return("hello "+id)
|
###(3)下面的代码同样可以获取路由中的值,如在路径中输入http://127.0.0.1:5000/hello?id=123,页面将会显示返回值`hello 123`
1 2 3 4 5 6 7
| from flask import Flask,request app = Flask(__name__)
@app.route('/hello') def hello(): id = request.args.get('id') return("hello "+id)
|
3.flask反向路由
1 2 3 4 5 6 7 8 9 10 11
| from flask import Flask,url_for app = Flask(__name__)
@app.route('/hello') def hello(): return("hello")
@app.route('/hello_url') def hello1(): return("hello "+url_for('hello'))
|
4.前后端数据传输
###(1)后端向前端传输数据
hello csdn
hello 1
后端代码
1 2 3 4 5 6 7 8 9
| from flask import Flask,render_template app = Flask(__name__)
@app.route('/hello') def hello(): user = User(1,"csdn") context = {"user":user} return render_template("hello.html",context=context)
|
前端代码
1 2 3 4 5 6 7 8 9 10 11 12
| (文件名为hello.html) <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>user_index</title> </head> <body> <h1>Hello {{ context.user.user_name }}</h1> <h1>Hello {{ context.user.user_id }}</h1> </body> </html>
|
###(2)前端向后端提交数据
下面的代码最后在在路径中输入http://127.0.0.1:5000/hello即可返回一个表单,`if request.method == ‘GET’:`用来判断是否是第一次请求,当时第一次请求时将返回一个空表单,如不是则将表单提交到后端进行处理
后端代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| from flask import Flask,render_template,request,flash app = Flask(__name__) app.secret_key = '123'
@app.route("/hello",methods=['POST','GET']) def login_index(): if request.method == 'GET': return render_template('hello.html') else: form = request.form user_name = form.get("user_name") password = form.get("password") if not user_name: flash("Please input username") return render_template("hello.html") if not password: flash("Please input password") return render_template("hello.html")
if user_name == "csdn" and password == "12345678": flash("Login succeed") return render_template("hello.html") else: flash("username or password is wrong") return render_template("hello.html")
|
前端代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| (文件名为hello.html) <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>Hello</h1> <form action="/login" method="post"> <input type="text" name="user_name"> <input type="password" name="password"> <input type="submit" name="submit" value="submit"> </form> <h2>{{ get_flashed_messages()[0] }}</h2> </body> </html>
|
5.消息提醒
####(1)使用消息提示是需要配置secret_key
####(2)flash获得的为一个列表,所以前端使用get_flashed_messages()[0]
来获得消息提醒
后端代码
1 2 3 4 5 6 7 8
| from flask import Flask,render_template,flash app = Flask(__name__) app.secret_key = '123'
@app.route("/hello") def hello(): flash("csdn") return render_template("hello.html")
|
前端代码
1 2 3 4 5 6 7 8 9 10 11 12
| (文件名为hello.html) <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>Hello</h1> <h2>{{ get_flashed_messages()[0] }}</h2> </body> </html>
|
6.异常处理
###(1)404错误
当路由中输入错误路径,将会返回404页面
后端代码
1 2 3 4 5 6
| from flask import Flask,render_template app = Flask(__name__)
@app.errorhandler(404) def hello(error): return render_template("404.html")
|
前端代码
1 2 3 4 5 6 7 8 9 10 11
| (文件名为404.html) <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>抱歉您找的页面不存在</h1> </body> </html>
|
###(2)有条件的跳转到404页面
####后端代码
1 2 3 4 5 6 7 8 9
| from flask import Flask,render_template,abort app = Flask(__name__)
@app.route("/hello/<id>") def hello(id): if int(id) == 1: return render_template("hello.html") else: abort(404)
|
前端代码
1 2 3 4 5 6 7 8 9 10 11
| (文件名为hello.html) <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>您已登录成功</h1> </body> </html>
|