一 flask 介紹

flask是一款micro web 服務器

介紹

輕量級服務器是指服務器剔除了一些功能,這樣服務器在部署,運行時開銷就會降低,變得很輕。
這裏的micro web 服務器是指flask爲了保持core簡單,但是功能是可以擴展的。也就是說flask只提供了核心的web server 功能。
flask沒有提供database abstraction layer, form validation, upload handling等其它功能,但是卻提供了擴展來支持這些功能。

1 開始Hello, World!

code

from flask import Flask

app = Flask(name)
#Flask 實例實現了WSGI(Web Server Gateway Interface)接口,
name作爲參數是爲了查找templates, static files等等。更多的信息參考flask文檔。

@app.route('/')
def hello_world():
return 'Hello, World!'
#使用route decorator使用指定的URL來觸發function

run server

set FLASK_APP=01_hello.py
set FLASK_ENV=development
開啓debug模式
flask run --host=0.0.0.0
指定server在所有public IPs監聽

browser

http://127.0.0.1:5000/

2 Routing

使用 route() decorator 綁定a function 到 a URL.br/>@app.route('/')
def hello_world():
return 'Hello, World!'

3 Variable Rules

位URL指定參數,可選地還可以指定類型。最後傳給函數。

@app.route('/user/<username>')
def show_user_profile(username):
return 'User %s' % (username,)

@app.route('/post/<int:post_id>')
def show_post(post_id):
return 'post %d' % (post_id,)

案例:br/>@app.route('/user/<username>')
def show_user_profile(username):
return 'User %s' % (username,)

@app.route('/post/<int:post_id>')
def show_user_profile(post_id):
return 'post %d' % (post_id,)
報錯:
AssertionError: View function mapping is overwriting an existing endpoint function: show_user_profile
原因:
兩個URL綁定到一個view function, flask認爲我們隊view function進行了overwriting

4 Unique URLs / Redirection Behavior

@app.route('/projects/')
如果請求URL爲/projects,這是會redirect到/projects/
請求會響應一個308,然後再響應一個200

note: URL 匹配規則爲由上向下

案例:br/>@app.route('/post/<int:post_id>/')
def show_post(post_id):
return 'post %d' % (post_id,)

@app.route('/post/<int:post_id>')
def show_post_profile(post_id):
return 'post profile %d' % (post_id,)

結果:
不管請求是post/2 還是post/2/,只有show_post響應。

方案: 調換兩個view function的順序。

5 URL Building

建立一個對應function的URL
用法:
第一個參數: function name
後面的key arguments對應URL variables
未知的arguments對應URL query variables

案例:
with app.test_request_context():
print(url_for('index'))
print(url_for('show_user_profile',username='test'))
print(url_for('show_user_profile',username='test',key='value'))
輸出:
/
/user/test
/user/test?key=value

6 HTTP Methods

處理不同的請求,需要爲route()指定methods參數

案例:br/>@app.route('/login')
def login():
return render_template('login.html')
請求:
http://127.0.0.1:5000/login
輸出:
405 Method Not Allowed
原因:
@app.route('/login') 默認只處理GET請求,不處理POST請求

7 Rendering Templates

使用render_template函數render a template
第一個參數:html template
後面的key arguments,爲傳遞給template的變量

上下文傳遞:
server -> template
browser form, URL variables -> server
redirect(URL Building) --> browser

案例:
def index():
user = ''
if request.method == 'POST':
user = request.form['user']
return render_template('index.html',user=user)
else:
return render_template('index.html',user=user)

<!doctype html>
<title> Hello from Flask</title>
<% if user %>
<h1> Hello {{ user }}!</h1>
<% else %>
<h1> index page!</h1>
<% endif %>

8 Accessing Request Data

context locals

9 The Request Objec

通過全局變量request獲取browser傳遞的消息
例如:
form:
request.form['username']
parameters:
searchword = request.args.get('key', '')
cooks:
headers:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章