轉(zhuǎn)換器 | 描述 |
---|---|
string | (缺省值) 接受任何不包含斜杠的文本 |
int | 接受正整數(shù) |
float | 接受正浮點(diǎn)數(shù) |
path | 類似 string ,但可以包含斜杠 |
uuid | 接受 UUID 字符串 |
#!/usr/bin/python # -*- coding: UTF-8 -*- """ @author:chenshifeng @file:flask_demo.py @time:2021/03/01 """ from flask import Flask app = Flask(__name__) @app.route('/post/int:post_id>') def show_post(post_id): # show the post with the given id, the id is an integer return 'Post %d' % post_id @app.route('/path/path:subpath>') def show_subpath(subpath): # show the subpath after /path/ return 'Subpath %s' % subpath if __name__ == '__main__': app.run(debug=True)
以下兩條規(guī)則的不同之處在于是否使用尾部的斜杠。:
@app.route('/projects/') def projects(): return 'The project page' @app.route('/about') def about(): return 'The about page'
projects 的 URL 是中規(guī)中矩的,尾部有一個(gè)斜杠,看起來就如同一個(gè)文件夾。 訪問一個(gè)沒有斜杠結(jié)尾的 URL 時(shí) Flask 會自動(dòng)進(jìn)行重定向,幫你在尾部加上一個(gè)斜杠。
about 的 URL 沒有尾部斜杠,因此其行為表現(xiàn)與一個(gè)文件類似。如果訪問這個(gè) URL 時(shí)添加了尾部斜杠就會得到一個(gè) 404 錯(cuò)誤。這樣可以保持 URL 唯一,并幫助 搜索引擎避免重復(fù)索引同一頁面。
url_for()函數(shù)對于動(dòng)態(tài)構(gòu)建特定函數(shù)的URL非常有用。該函數(shù)接受函數(shù)的名稱作為第一個(gè)參數(shù),以及一個(gè)或多個(gè)關(guān)鍵字參數(shù),每個(gè)參數(shù)對應(yīng)于URL的變量部分。
#!/usr/bin/python # -*- coding: UTF-8 -*- """ @author:chenshifeng @file:flask_demo.py @time:2021/03/01 """ from flask import Flask, redirect, url_for app = Flask(__name__) @app.route('/admin') def hello_admin(): return 'Hello Admin' @app.route('/guest/guest>') def hello_guest(guest): return 'Hello %s as Guest' % guest @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)) if __name__ == '__main__': app.run(debug=True)
redirect函數(shù)用于重定向,實(shí)現(xiàn)機(jī)制很簡單,就是向客戶端(瀏覽器)發(fā)送一個(gè)重定向的HTTP報(bào)文,瀏覽器會去訪問報(bào)文中指定的url。
運(yùn)行
打開瀏覽器并輸入U(xiǎn)RL - http://localhost:5000/user/admin
Hello Admin
在瀏覽器中輸入以下URL - http://localhost:5000/user/mvl
Hello mvl as Guest
Web 應(yīng)用使用不同的 HTTP 方法處理 URL 。當(dāng)你使用 Flask 時(shí),應(yīng)當(dāng)熟悉 HTTP 方法。 缺省情況下,一個(gè)路由只回應(yīng) GET 請求。 可以使用 route() 裝飾器的 methods 參數(shù)來處理不同的 HTTP 方法:
方法 | 描述 |
---|---|
GET | 以未加密的形式將數(shù)據(jù)發(fā)送到服務(wù)器,最常見的方法。 |
HEAD | 和GET方法相同,但沒有響應(yīng)體。 |
POST | 用于將HTML表單數(shù)據(jù)發(fā)送到服務(wù)器,POST方法接收的數(shù)據(jù)不由服務(wù)器緩存。 |
PUT | 用上傳的內(nèi)容替換目標(biāo)資源的所有當(dāng)前表示。 |
DELETE | 刪除由URL給出的目標(biāo)資源的所有當(dāng)前表示。 |
默認(rèn)情況下,F(xiàn)lask路由響應(yīng)GET請求。但是,可以通過為route()裝飾器提供方法參數(shù)來更改此首選項(xiàng)。
為了演示在URL路由中使用POST方法,首先讓我們創(chuàng)建一個(gè)HTML表單,并使用POST方法將表單數(shù)據(jù)發(fā)送到URL。
將以下腳本另存為login.html
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>
運(yùn)行以下代碼
from flask import Flask, redirect, url_for, request app = Flask(__name__) @app.route('/success/name>') def success(name): return 'welcome %s' % name @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)) if __name__ == '__main__': app.run(debug = True)
在瀏覽器中打開login.html,在文本字段中輸入name,然后單擊提交。
表單數(shù)據(jù)將POST到表單標(biāo)簽的action子句中的URL。
http://localhost/login映射到login()函數(shù)。由于服務(wù)器通過POST方法接收數(shù)據(jù),因此通過以下步驟獲得從表單數(shù)據(jù)獲得的“nm”參數(shù)的值:
表單數(shù)據(jù)將POST到表單標(biāo)簽的action子句中的URL。
user = request.form['nm']
它作為變量部分傳遞給'/ success' URL。瀏覽器在窗口中顯示welcome消息。
在login.html中將方法參數(shù)更改為'GET',然后在瀏覽器中再次打開它。服務(wù)器上接收的數(shù)據(jù)是通過GET方法獲得的。通過以下的步驟獲得'nm'參數(shù)的值:
User = request.args.get('nm')
這里,args是包含表單參數(shù)對及其對應(yīng)值對的列表的字典對象。與'nm'參數(shù)對應(yīng)的值將像之前一樣傳遞到'/ success' URL。
在大型應(yīng)用中,把業(yè)務(wù)邏輯和表現(xiàn)內(nèi)容放在一起,會增加代碼的復(fù)雜度和維護(hù)成本.
使用模板的好處
視圖函數(shù)只負(fù)責(zé)業(yè)務(wù)邏輯和數(shù)據(jù)處理(業(yè)務(wù)邏輯方面)而模板則取到視圖函數(shù)的數(shù)據(jù)結(jié)果進(jìn)行展示(視圖展示方面)代碼結(jié)構(gòu)清晰,耦合度低
使用 render_template() 方法可以渲染模板,你只要提供模板名稱和需要 作為參數(shù)傳遞給模板的變量就行了。
Flask 會在 templates 文件夾內(nèi)尋找模板。因此,如果你的應(yīng)用是一個(gè)模塊, 那么模板文件夾應(yīng)該在模塊旁邊;如果是一個(gè)包,那么就應(yīng)該在包里面:
情形 1 : 一個(gè)模塊:
/application.py /templates /hello.html
情形 2 : 一個(gè)包:
/application /__init__.py /templates /hello.html
示例代碼:
from flask import Flask, render_template app = Flask(__name__) @app.route('/') def index(): my_int = 18 my_str = 'curry' my_list = [1, 5, 4, 3, 2] my_dict = { 'name': 'durant', 'age': 28 } # render_template方法:渲染模板 # 參數(shù)1: 模板名稱 參數(shù)n: 傳到模板里的數(shù)據(jù) return render_template('hello.html', my_int=my_int, my_str=my_str, my_list=my_list, my_dict=my_dict) if __name__ == '__main__': app.run(debug=True)
!DOCTYPE html> html lang="en"> head> meta charset="UTF-8"> title>Title/title> /head> body> h2>我是模板/h2> {{ my_int }} br> {{ my_str }} br> {{ my_list }} br> {{ my_dict }} hr> h2>模板的list數(shù)據(jù)獲取/h2> hr> {{ my_list[0] }} br> {{ my_list.1 }} hr> h2>字典數(shù)據(jù)獲取/h2> hr> {{ my_dict['name'] }} br> {{ my_dict.age }} hr> h2>算術(shù)運(yùn)算/h2> br> {{ my_list.0 + 10 }} br> {{ my_list[0] + my_list.1 }} /body> /html>
運(yùn)行效果:
動(dòng)態(tài)的 web 應(yīng)用也需要靜態(tài)文件,一般是 CSS 和 JavaScript 文件。理想情況下你的 服務(wù)器已經(jīng)配置好了為你的提供靜態(tài)文件的服務(wù)。但是在開發(fā)過程中, Flask 也能做好 這項(xiàng)工作。只要在你的包或模塊旁邊創(chuàng)建一個(gè)名為 static 的文件夾就行了。 靜態(tài)文件位于應(yīng)用的 /static 中。
使用特定的 'static' 端點(diǎn)就可以生成相應(yīng)的 URL
url_for('static', filename='style.css')
這個(gè)靜態(tài)文件在文件系統(tǒng)中的位置應(yīng)該是 static/style.css 。
在下面的示例中,在index.html中的HTML按鈕的OnClick事件上調(diào)用hello.js中定義的javascript函數(shù),該函數(shù)在Flask應(yīng)用程序的“/”URL上呈現(xiàn)。
from flask import Flask, render_template app = Flask(__name__) @app.route("/") def index(): return render_template("index.html") if __name__ == '__main__': app.run(debug = True)
index.html的HTML腳本如下所示:
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>
Hello.js包含sayHello()函數(shù)。
function sayHello() { alert("Hello World") }
運(yùn)行效果:
來自客戶端網(wǎng)頁的數(shù)據(jù)作為全局請求對象發(fā)送到服務(wù)器。為了處理請求數(shù)據(jù),應(yīng)該從Flask模塊導(dǎo)入。
Request對象的重要屬性如下所列:
首先,你必須從 flask 模塊導(dǎo)入請求對象:
from flask import request
我們已經(jīng)看到,可以在 URL 規(guī)則中指定 http 方法。觸發(fā)函數(shù)接收的 Form 數(shù)據(jù)可以以字典對象的形式收集它并將其轉(zhuǎn)發(fā)到模板以在相應(yīng)的網(wǎng)頁上呈現(xiàn)它。
在以下示例中,'/' URL 會呈現(xiàn)具有表單的網(wǎng)頁(student.html)。填入的數(shù)據(jù)會發(fā)布到觸發(fā) result() 函數(shù)的 '/result' URL。
result() 函數(shù)收集字典對象中的 request.form 中存在的表單數(shù)據(jù),并將其發(fā)送給 result.html。
該模板動(dòng)態(tài)呈現(xiàn)表單數(shù)據(jù)的 HTML 表格。
下面給出的是應(yīng)用程序的 Python 代碼:
from flask import Flask, render_template, request app = 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.form return render_template("result.html",result = result) if __name__ == '__main__': app.run(debug = True)
下面給出的是 student.html 的 HTML 腳本。
!DOCTYPE html> html lang="en"> head> meta charset="UTF-8"> title>Title/title> /head> body> 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> /body> /html>
下面給出了模板( result.html )的代碼:
!doctype html> table border = 1> {% for key, value in result.items() %} tr> th> {{ key }} /th> td> {{ value }}/td> /tr> {% endfor %} /table>
運(yùn)行效果:
運(yùn)行 Python 腳本,并在瀏覽器中輸入 URL http://localhost:5000/。
當(dāng)點(diǎn)擊提交按鈕時(shí),表單數(shù)據(jù)以 HTML 表格的形式呈現(xiàn)在 result.html 上。
Cookie以文本文件的形式存儲在客戶端的計(jì)算機(jī)上。其目的是記住和跟蹤與客戶使用相關(guān)的數(shù)據(jù),以獲得更好的訪問者體驗(yàn)和網(wǎng)站統(tǒng)計(jì)信息。
Request對象包含Cookie的屬性。它是所有cookie變量及其對應(yīng)值的字典對象,客戶端已傳輸。除此之外,cookie還存儲其網(wǎng)站的到期時(shí)間,路徑和域名。
在Flask中,對cookie的處理步驟為:
1.設(shè)置cookie:
設(shè)置cookie,默認(rèn)有效期是臨時(shí)cookie,瀏覽器關(guān)閉就失效
可以通過 max_age 設(shè)置有效期, 單位是秒
cookie_1 = request.cookies.get("chenshifeng")
2.獲取cookie
獲取cookie,通過request.cookies的方式, 返回的是一個(gè)字典,可以獲取字典里的相應(yīng)的值
resp = make_response("del success") # 設(shè)置響應(yīng)體 resp.delete_cookie("chenshifeng")
3.刪除cookie
這里的刪除只是讓cookie過期,并不是直接刪除cookie
刪除cookie,通過delete_cookie()的方式, 里面是cookie的名字
resp = make_response("del success") # 設(shè)置響應(yīng)體 resp.delete_cookie("chenshifeng")
以下為Flask Cookies的簡單示例:
from flask import Flask, make_response, request app = Flask(__name__) @app.route("/set_cookies") def set_cookie(): resp = make_response("success") resp.set_cookie("chenshifeng", "shifengboy",max_age=3600) return resp @app.route("/get_cookies") def get_cookie(): cookie_1 = request.cookies.get("chenshifeng") # 獲取名字為Itcast_1對應(yīng)cookie的值 return cookie_1 @app.route("/delete_cookies") def delete_cookie(): resp = make_response("del success") resp.delete_cookie("chenshifeng") return resp if __name__ == '__main__': app.run(debug=True)
設(shè)置cookies
運(yùn)行應(yīng)用程序,在瀏覽器中輸入 127.0.0.1:5000/set_cookies 來設(shè)置cookies,設(shè)置 cookies 的輸出如下所示:
獲取cookie
根據(jù)視圖函數(shù)中相對應(yīng)的路徑,輸入 http://127.0.0.1:5000/get_cookies ,讀回 cookies 的輸出如下所示:
刪除cookie
根據(jù)視圖函數(shù)中相對應(yīng)的路徑,輸入 http://127.0.0.1:5000/delete_cookies ,刪除 cookies 的輸出如下所示:
注意刪除,只是讓 cookie 過期。
與Cookie不同,Session(會話)數(shù)據(jù)存儲在服務(wù)器上。會話是客戶端登錄到服務(wù)器并注銷服務(wù)器的時(shí)間間隔。需要在該會話中保存的數(shù)據(jù)會存儲在服務(wù)器上的臨時(shí)目錄中。
為每個(gè)客戶端的會話分配會話ID。會話數(shù)據(jù)存儲在cookie的頂部,服務(wù)器以加密方式對其進(jìn)行簽名。對于此加密,F(xiàn)lask應(yīng)用程序需要一個(gè)定義的SECRET_KEY。
Session對象也是一個(gè)字典對象,包含會話變量和關(guān)聯(lián)值的鍵值對。
例如,要設(shè)置一個(gè)'username'會話變量,請使用以下語句:
Session['username'] = 'admin'
要釋放會話變量,請使用pop()方法。
session.pop('username', None)
演示代碼:
from flask import Flask, session, redirect, url_for, request app = Flask(__name__) app.secret_key = 'fkdjsafjdkfdlkjfadskjfadskljdsfklj' # 確保設(shè)置應(yīng)用程序的secret_key @app.route('/') def index(): if 'username' in session: username = session['username'] return '登錄用戶名是:' + username + 'br>' + \ "b>a href = '/logout'>點(diǎn)擊這里注銷/a>/b>" return "您暫未登錄, br>a href = '/login'>/b>" + \ "點(diǎn)擊這里登錄/b>/a>" @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': session['username'] = request.form['username'] return redirect(url_for('index')) return ''' form action = "" method = "post"> p>input type ="text" name ="username"/>/p> p>input type ="submit" value ="登錄"/>/p> /form> ''' @app.route('/logout') def logout(): # remove the username from the session if it is there session.pop('username', None) return redirect(url_for('index')) if __name__ == '__main__': app.run(debug=True)
如何生成一個(gè)好的密鑰
生成隨機(jī)數(shù)的關(guān)鍵在于一個(gè)好的隨機(jī)種子,因此一個(gè)好的密鑰應(yīng)當(dāng)有足夠的隨機(jī)性。 操作系統(tǒng)可以有多種方式基于密碼隨機(jī)生成器來生成隨機(jī)數(shù)據(jù)。使用下面的命令 可以快捷的為 Flask.secret_key ( 或者 SECRET_KEY )生成值:
$ python -c 'import os; print(os.urandom(16))'
b'_5#y2L"F4Q8z\n\xec]/'
訪問http://127.0.0.1:5000/,只是提示用戶登錄,因?yàn)槲丛O(shè)置會話變量'username'。
當(dāng)用戶點(diǎn)擊登錄,瀏覽到“/login”login()視圖函數(shù)時(shí),因?yàn)樗峭ㄟ^GET方法調(diào)用的,所以將打開一個(gè)登錄表單。
點(diǎn)擊登錄,通過POST方法將表單發(fā)送回'/login',現(xiàn)在會話變量已設(shè)置。應(yīng)用程序重定向到'/'。此時(shí)會話變量'username'被找到。
應(yīng)用程序還包含一個(gè)logout()視圖函數(shù),它會彈出'username'會話變量。因此,'/' URL再次顯示開始頁面。
Flask類有一個(gè)redirect()函數(shù)。調(diào)用時(shí),它返回一個(gè)響應(yīng)對象,并將用戶重定向到具有指定狀態(tài)代碼的另一個(gè)目標(biāo)位置。
redirect()函數(shù)的原型如下:
Flask.redirect(location, statuscode, response)
在上述函數(shù)中:
以下狀態(tài)代碼已標(biāo)準(zhǔn)化:
默認(rèn)狀態(tài)代碼為302,表示'found'。
在以下示例中,redirect()函數(shù)用于在登錄嘗試失敗時(shí)再次顯示登錄頁面。
from flask import Flask, redirect, url_for, render_template, request # Initialize the Flask application app = Flask(__name__) @app.route('/') def index(): return render_template('log_in.html') @app.route('/login', methods=['POST', 'GET']) def login(): if request.method == 'POST' and request.form['username'] == 'admin': return redirect(url_for('success')) return redirect(url_for('index')) @app.route('/success') def success(): return 'logged in successfully' if __name__ == '__main__': app.run(debug=True)
下面給出的是 log_in.html的 HTML 腳本。
!DOCTYPE html> html lang="en"> head> meta charset="UTF-8"> title>Title/title> /head> body> form action = "http://localhost:5000/login" method = "POST"> p>Enter Name:/p> p>input type = "text" name = "username" />/p> p>input type = "submit" value = "submit" />/p> /form> /body> /html>
Flask類具有帶有錯(cuò)誤代碼的abort()函數(shù)。
Flask.abort(code)
Code參數(shù)采用以下值之一:
讓我們對上述代碼中的login()函數(shù)稍作更改。如果要顯示'Unauthurized'頁面,請將其替換為調(diào)用abort(401),而不是重新顯示登錄頁面。
from flask import Flask, redirect, url_for, render_template, request, abort app = Flask(__name__) @app.route('/') def index(): return render_template('log_in.html') @app.route('/login', methods=['POST', 'GET']) def login(): if request.method == 'POST': if request.form['username'] == 'admin': return redirect(url_for('success')) else: abort(401) else: return redirect(url_for('index')) @app.route('/success') def success(): return 'logged in successfully' if __name__ == '__main__': app.run(debug=True)
運(yùn)行,輸入非admin的用戶名,點(diǎn)擊提交
一個(gè)好的基于 GUI 的應(yīng)用程序會向用戶提供有關(guān)交互的反饋。例如,桌面應(yīng)用程序使用對話框或消息框,JavaScript 使用警報(bào)用于類似目的。
在 Flask Web 應(yīng)用程序中生成這樣的信息性消息很容易。Flask 框架的閃現(xiàn)系統(tǒng)可以在一個(gè)視圖中創(chuàng)建消息,并在名為 next 的視圖函數(shù)中呈現(xiàn)它。
Flask 模塊包含 flash() 方法。它將消息傳遞給下一個(gè)請求,該請求通常是一個(gè)模板。
flash(message, category)
其中,
以下是一個(gè)完整的示例:
from flask import Flask, flash, redirect, render_template, \ request, url_for app = Flask(__name__) app.secret_key = b'_5#y2L"F4Q8z\n\xec]/' @app.route('/') def index(): return render_template('index.html') @app.route('/login', methods=['GET', 'POST']) def login(): error = None if request.method == 'POST': if request.form['username'] != 'admin' or \ request.form['password'] != 'secret': error = 'Invalid credentials' else: flash('You were successfully logged in') return redirect(url_for('index')) return render_template('login.html', error=error) if __name__ == '__main__': app.run(debug=True)
以下是實(shí)現(xiàn)閃現(xiàn)的 layout.html 模板:
!doctype html> title>My Application/title> {% with messages = get_flashed_messages() %} {% if messages %} ul class=flashes> {% for message in messages %} li>{{ message }}/li> {% endfor %} /ul> {% endif %} {% endwith %} {% block body %}{% endblock %}
以下是繼承自 layout.html 的 index.html 模板:
{% block body %} h1>Overview/h1> p>Do you want to a href="{{ url_for('login') }}">log in?/a> {% endblock %}
以下是同樣繼承自 layout.html 的 login.html 模板:
{% extends "layout.html" %} {% block body %} h1>Login/h1> {% if error %} p class=error>strong>Error:/strong> {{ error }} {% endif %} form method=post> dl> dt>Username: dd>input type=text name=username value="{{ request.form.username }}"> dt>Password: dd>input type=password name=password> /dl> p>input type=submit value=Login> /form> {% endblock %}
運(yùn)行
首頁
登錄報(bào)錯(cuò)頁
登錄成功頁
在 Flask 中處理文件上傳非常簡單。它需要一個(gè) HTML 表單,其 enctype
屬性設(shè)置為'multipart/form-data'
,將文件發(fā)布到 URL。URL 處理程序從 request.files[]
對象中提取文件,并將其保存到所需的位置。
每個(gè)上傳的文件首先會保存在服務(wù)器上的臨時(shí)位置,然后將其實(shí)際保存到它的最終位置。目標(biāo)文件的名稱可以是硬編碼的,也可以從 request.files[file]
對象的filename
屬性中獲取。但是,建議使用 secure_filename()
函數(shù)獲取它的安全版本。
可以在 Flask 對象的配置設(shè)置中定義默認(rèn)上傳文件夾的路徑和上傳文件的最大大小。
app.config[‘UPLOAD_FOLDER'] 定義上傳文件夾的路徑 app.config[‘MAX_CONTENT_LENGTH'] 指定要上傳的文件的最大大?。ㄒ宰止?jié)為單位)
以下代碼具有 '/upload'
URL 規(guī)則,該規(guī)則在 templates 文件夾中顯示 'upload.html'
,以及 '/ upload-file'
URL 規(guī)則,用于調(diào)用 uploader()
函數(shù)處理上傳過程。
'upload.html'
有一個(gè)文件選擇器按鈕和一個(gè)提交按鈕。
html> head> title>File Upload/title> /head> body> form action="http://localhost:5000/uploader" method="POST" enctype="multipart/form-data"> input type="file" name="file" accept=".jpg,.png" /> input type="submit" /> /form> /body> /html>
以下是 Flask 應(yīng)用程序的 Python 代碼。
from flask import Flask, render_template, request from werkzeug.utils import secure_filename import os app = Flask(__name__) app.config['UPLOAD_FOLDER'] = '/Users/chenshifeng/upload/' @app.route('/upload') def upload_file(): return render_template('upload.html') @app.route('/uploader', methods = ['GET', 'POST']) def uploader(): if request.method == 'POST': f = request.files['file'] f.save(os.path.join(app.config['UPLOAD_FOLDER'],secure_filename(f.filename))) return 'file uploaded successfully' if __name__ == '__main__': app.run()
您將看到如下所示的界面。
選擇文件后,單擊提交。表單的 post 方法調(diào)用 '/ upload_file'
URL。底層函數(shù) uploader()
執(zhí)行保存操作。
上傳成功會顯示以下畫面:
上傳文件被放到/Users/chenshifeng/upload文件夾下:
Flask通常被稱為微框架,因?yàn)楹诵墓δ馨ɑ赪erkzeug的WSGI和路由以及基于Jinja2的模板引擎。此外,F(xiàn)lask框架還支持cookie和會話,以及JSON,靜態(tài)文件等Web幫助程序。顯然,這不足以開發(fā)完整的Web應(yīng)用程序。而Flask擴(kuò)展就具備這樣的功能。Flask擴(kuò)展為Flask框架提供了可擴(kuò)展性。
有大量的Flask擴(kuò)展可用。Flask擴(kuò)展是一個(gè)Python模塊,它向Flask應(yīng)用程序添加了特定類型的支持。Flask Extension Registry(Flask擴(kuò)展注冊表)是一個(gè)可用的擴(kuò)展目錄??梢酝ㄟ^pip實(shí)用程序下載所需的擴(kuò)展名。
在本教程中,我們將討論以下重要的Flask擴(kuò)展:
每種類型的擴(kuò)展通常提供有關(guān)其用法的大量文檔。由于擴(kuò)展是一個(gè)Python模塊,因此需要導(dǎo)入它才能使用它。Flask擴(kuò)展名通常命名為flask-foo。導(dǎo)入的操作如下:
from flask_foo import [class, function]
對于0.7以后的Flask版本,您還可以使用語法:
from flask.ext import foo
對于此用法,需要激活兼容性模塊。它可以通過運(yùn)行flaskext_compat.py來安裝:
import flaskext_compat flaskext_compat.activate() from flask.ext import foo
務(wù)器建立簡單的接口變得非常容易。
首先,應(yīng)該在pip實(shí)用程序的幫助下安裝Flask-Mail擴(kuò)展。
pip install Flask-Mail
然后需要通過設(shè)置以下應(yīng)用程序參數(shù)的值來配置Flask-Mail。
參數(shù) | 描述 |
---|---|
MAIL_SERVER | 電子郵件服務(wù)器的名稱/IP地址 |
MAIL_PORT | 使用的服務(wù)器的端口號 |
MAIL_USE_TLS | 啟用/禁用傳輸安全層加密 |
MAIL_USE_SSL | 啟用/禁用安全套接字層加密 |
MAIL_DEBUG | 調(diào)試支持。默認(rèn)值是Flask應(yīng)用程序的調(diào)試狀態(tài) |
MAIL_USERNAME | 發(fā)件人的用戶名 |
MAIL_PASSWORD | 發(fā)件人的密碼 |
MAIL_DEFAULT_SENDER | 設(shè)置默認(rèn)發(fā)件人 |
MAIL_MAX_EMAILS | 設(shè)置要發(fā)送的最大郵件數(shù) |
MAIL_SUPPRESS_SEND | 如果app.testing設(shè)置為true,則發(fā)送被抑制 |
MAIL_ASCII_ATTACHMENTS | 如果設(shè)置為true,則附加的文件名將轉(zhuǎn)換為ASCII |
flask-mail模塊包含以下重要類的定義。
14.1.1 Mail類
它管理電子郵件消息傳遞需求。類構(gòu)造函數(shù)采用以下形式:
flask-mail.Mail(app = None)
構(gòu)造函數(shù)將Flask應(yīng)用程序?qū)ο笞鳛閰?shù)。
Mail類的方法:
方法 | 描述 |
---|---|
send() | 發(fā)送Message類對象的內(nèi)容 |
connect() | 打開與郵件主機(jī)的連接 |
send_message() | 發(fā)送消息對象 |
14.1.2 Message類
它封裝了一封電子郵件。Message類構(gòu)造函數(shù)有幾個(gè)參數(shù):
flask-mail.Message(subject, recipients, body, html, sender, cc, bcc, reply-to, date, charset, extra_headers, mail_options,rcpt_options)
Message類方法:
attach() - 為郵件添加附件。此方法采用以下參數(shù):
filename - 要附加的文件的名稱content_type - MIME類型的文件data - 原始文件數(shù)據(jù)處置 - 內(nèi)容處置(如果有的話)。
add_recipient() - 向郵件添加另一個(gè)收件人
在下面的示例中,使用QQ郵箱服務(wù)的SMTP服務(wù)器用作Flask-Mail配置的MAIL_SERVER。
from flask import Flask from flask_mail import Mail, Message app =Flask(__name__) mail=Mail(app) app.config['MAIL_SERVER']='smtp.qq.com' app.config['MAIL_PORT'] = 465 app.config['MAIL_USERNAME'] = 'xxxxx@qq.com' app.config['MAIL_PASSWORD'] = 'xxxxxxxx' app.config['MAIL_USE_TLS'] = False app.config['MAIL_USE_SSL'] = True mail = Mail(app) @app.route("/") def index(): msg = Message('Hello', sender = 'xxxxx@qq.com', recipients = ['xxxxx@qq.com']) msg.body = "Hello Flask message sent from Flask-Mail" mail.send(msg) return "Sent" if __name__ == '__main__': app.run(debug = True)
步驟1 - 安裝Flask-SQLAlchemy擴(kuò)展。
pip install flask-sqlalchemy
步驟2 - 您需要從此模塊導(dǎo)入SQLAlchemy類。
from flask_sqlalchemy import SQLAlchemy
步驟3 - 現(xiàn)在創(chuàng)建一個(gè)Flask應(yīng)用程序?qū)ο蟛橐褂玫臄?shù)據(jù)庫設(shè)置URI。
app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///students.sqlite3'
步驟4 - 然后使用應(yīng)用程序?qū)ο笞鳛閰?shù)創(chuàng)建SQLAlchemy類的對象。該對象包含用于ORM操作的輔助函數(shù)。它還提供了一個(gè)父Model類,使用它來聲明用戶定義的模型。在下面的代碼段中,創(chuàng)建了students模型。
db = SQLAlchemy(app) class students(db.Model): id = db.Column('student_id', db.Integer, primary_key = True) name = db.Column(db.String(100)) city = db.Column(db.String(50)) addr = db.Column(db.String(200)) pin = db.Column(db.String(10)) def __init__(self, name, city, addr,pin): self.name = name self.city = city self.addr = addr self.pin = pin
步驟5 - 要?jiǎng)?chuàng)建/使用URI中提及的數(shù)據(jù)庫,請運(yùn)行create_all()方法。
db.create_all()
SQLAlchemy的Session對象管理ORM對象的所有持久性操作。
以下session方法執(zhí)行CRUD操作:
您可以通過使用filter屬性將過濾器應(yīng)用于檢索到的記錄集。例如,要在學(xué)生表中檢索city ='Hyderabad'的記錄,請使用以下語句:
Students.query.filter_by(city = 'Hyderabad').all()
有了這么多的背景,現(xiàn)在我們將為我們的應(yīng)用程序提供視圖函數(shù)來添加學(xué)生數(shù)據(jù)。
應(yīng)用程序的入口點(diǎn)是綁定到'/' URL的show_all()函數(shù)。學(xué)生表的記錄集作為參數(shù)發(fā)送到HTML模板。模板中的服務(wù)器端代碼以HTML表格形式呈現(xiàn)記錄。
@app.route('/') def show_all(): return render_template('show_all.html', students = students.query.all() )
模板('show_all.html')的HTML腳本如下:
!DOCTYPE html> html lang = "en"> head>/head> body> h3> a href = "{{ url_for('show_all') }}">Comments - Flask SQLAlchemy example/a> /h3> hr/> {%- for message in get_flashed_messages() %} {{ message }} {%- endfor %} h3>Students (a href = "{{ url_for('new') }}">Add Student /a>)/h3> table> thead> tr> th>Name/th> th>City/th> th>Address/th> th>Pin/th> /tr> /thead> tbody> {% for student in students %} tr> td>{{ student.name }}/td> td>{{ student.city }}/td> td>{{ student.addr }}/td> td>{{ student.pin }}/td> /tr> {% endfor %} /tbody> /table> /body> /html>
上述網(wǎng)頁包含指向'/ new' URL映射new()函數(shù)的超鏈接。單擊時(shí),將打開“學(xué)生信息”表單。 數(shù)據(jù)在 POST方法中發(fā)布到相同的URL。
new.html
!DOCTYPE html> html> body> h3>Students - Flask SQLAlchemy example/h3> hr/> {%- for category, message in get_flashed_messages(with_categories = true) %} div class = "alert alert-danger"> {{ message }} /div> {%- endfor %} form action = "{{ request.path }}" method = "post"> label for = "name">Name/label>br> input type = "text" name = "name" placeholder = "Name" />br> label for = "email">City/label>br> input type = "text" name = "city" placeholder = "city" />br> label for = "addr">addr/label>br> textarea name = "addr" placeholder = "addr">/textarea>br> label for = "PIN">PINCODE/label>br> input type = "text" name = "pin" placeholder = "pin" />br> input type = "submit" value = "Submit" /> /form> /body> /html>
當(dāng)http方法被檢測為POST時(shí),表單數(shù)據(jù)被添加到學(xué)生表中,并且應(yīng)用返回到顯示添加數(shù)據(jù)的主頁。
@app.route('/new', methods = ['GET', 'POST']) def new(): if request.method == 'POST': if not request.form['name'] or not request.form['city'] or not request.form['addr']: flash('Please enter all the fields', 'error') else: student = students(request.form['name'], request.form['city'], request.form['addr'], request.form['pin']) db.session.add(student) db.session.commit() flash('Record was successfully added') return redirect(url_for('show_all')) return render_template('new.html')
下面給出了應(yīng)用程序(app.py)的完整代碼。
from flask import Flask, request, flash, url_for, redirect, render_template from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///students.sqlite3' app.config['SECRET_KEY'] = "random string" db = SQLAlchemy(app) class students(db.Model): id = db.Column('student_id', db.Integer, primary_key=True) name = db.Column(db.String(100)) city = db.Column(db.String(50)) addr = db.Column(db.String(200)) pin = db.Column(db.String(10)) def __init__(self, name, city, addr, pin): self.name = name self.city = city self.addr = addr self.pin = pin @app.route('/') def show_all(): return render_template('show_all.html', students=students.query.all()) @app.route('/new', methods=['GET', 'POST']) def new(): if request.method == 'POST': if not request.form['name'] or not request.form['city'] or not request.form['addr']: flash('Please enter all the fields', 'error') else: student = students(request.form['name'], request.form['city'], request.form['addr'], request.form['pin']) db.session.add(student) db.session.commit() flash('Record was successfully added') return redirect(url_for('show_all')) return render_template('new.html') if __name__ == '__main__': db.create_all() app.run(debug=True)
首頁
錯(cuò)誤頁:
提交成功頁:
本文參考:https://www.w3cschool.cn/flask/
到此這篇關(guān)于python flask框架詳解的文章就介紹到這了,更多相關(guān)python flask框架內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:西寧 石家莊 梅州 文山 懷化 浙江 昆明 錫林郭勒盟
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python flask框架詳解》,本文關(guān)鍵詞 python,flask,框架,詳解,python,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。