flask使用
文章目錄
- 基本實現(xiàn)過程
- 應(yīng)用
- 從 url 接收變量
- 構(gòu)建url
- html 向 路由發(fā)送信息
- html 和路由之間傳送數(shù)據(jù)
- 按鈕觸發(fā)事件
- 頁面顯示表單
- 在服務(wù)器運(yùn)行代碼
基本實現(xiàn)過程
應(yīng)用
from flask import Flask app = Flask(__name__)@app.route('/') def hello_world():return 'Hello World'if __name__ == '__main__':app.run(host = '0.0.0.0', port = 5000)應(yīng)用主體是Flask(__name__)
route 是路由,表示到了哪一步需要調(diào)用哪個函數(shù)
必須有主函數(shù),調(diào)用run
從 url 接收變量
@app.route('/hello/<name>')當(dāng)輸入 url / hello / myname 時 name 會直接獲取 myname的數(shù)據(jù)
構(gòu)建url
從一個界面跳轉(zhuǎn)到另外一個界面
@app.route('/user/<name>') def hello_user(name):if name =='admin':return redirect(url_for('hello_admin'))else:return redirect(url_for('hello_guest',guest = name))需要引入 flask 中的 redirect 和 url_for 模塊 ,url_for傳入函數(shù)名和函數(shù)對應(yīng)需要的參數(shù)
html 向 路由發(fā)送信息
<html><body><form action = "http://localhost:5000/login" method = "post"><p>Enter Name:</p><p><input type = "text" name = "nm" /></p><p><input type = "submit" value = "submit" /></p></form></body> </html>action 是 路由位置,method 是傳輸?shù)姆绞?/p> @app.route('/login',methods = ['POST', 'GET']) def login():if request.method == 'POST':user = request.form['nm']return redirect(url_for('success',name = user))else:user = request.args.get('nm')return redirect(url_for('success',name = user))
需要從 flask 中導(dǎo)入 request 模塊
默認(rèn)是用request模塊接收數(shù)據(jù)
html 和路由之間傳送數(shù)據(jù)
從 flask 導(dǎo)入模塊 render_template 并在主程序同一級建立文件夾templates,所有的html文件放在里面
<html lang="en"> <head><meta charset=UTF-8"><title>Title</title> </head> <body> 模板內(nèi)容 </body> </html>示例html
from flask import Flask, render_template app = Flask(__name__)@app.route('/') def index():return render_template('login.html')if __name__ == '__main__':app.run(debug=True) <html lang="en"> <head><meta charset=UTF-8"><title>Title</title> </head> <body> <br>{{ dic }} <br>{{ ls }} <br>{{ string }} <br>{{ num }} <br> </body> </html>想要接收的變量要寫成 {{ name }} 空格,左右括號的格式絕對不能變
@app.route('/') def index():dic = {1: 'a', 2: 'b'}ls = [1, 2, 3]string = 'nihoa'num = 12return render_template('login.html',dic = dic,ls = ls,string = string,num = num)傳遞參數(shù)的方式依舊是render_template, 特定參數(shù)的傳輸用 等號
按鈕觸發(fā)事件
<html> <head><script type = "text/javascript" src = "{{ url_for('static', filename = 'hello.js') }}"></script> </head><body> <input type="button" onclick="sayHello()" value="Say hello"/> </body> </html>按鈕有函數(shù)onclick
from flask import Flask, render_templateapp = Flask(__name__)@app.route('/') def index():return render_template('login.html')if __name__ == '__main__':app.run(debug=True) function sayHello() {alter("Hello World") }問題:運(yùn)行時出現(xiàn)get js 404
頁面顯示表單
from flask import Flask, render_template, requestapp = Flask(__name__)@app.route('/') def student():return render_template('student.html')@app.route('/result', methods=['POST', 'GET']) def result():if request.method == 'POST':result = request.formreturn render_template("result.html", result=result)if __name__ == '__main__':app.run(debug=True)注意在url旁邊的是methods復(fù)數(shù)拼寫
<form action = "http://localhost:5000/result" method = "POST"><p>Name <input type="text" name = "Name" /></p><p>Physics <input type="text" name = "Physics" /></p><p>Chemistry <input type = "text" name = "chemistry" /></p><p>Maths <input type = "text" name = "Mathematics /"/></p><p><input type = "submit" value = "submit" /></p> </form>input 在 html 語法中不需要反對應(yīng),只有一行的話用/就夠了
input 有 type 和 name 兩種類型,name相當(dāng)于是關(guān)鍵字,
在form中自動形成了字典
tr 表示 在一行 th表示行首,td表示行末
在服務(wù)器運(yùn)行代碼
想要在服務(wù)器上運(yùn)行flask代碼首先要在服務(wù)器上部署flask
總結(jié)
- 上一篇: 直方图均衡
- 下一篇: codeblock 显示 no such