一flask介紹-創(chuàng)新互聯(lián)

flask是一款micro web 服務(wù)器 介紹

輕量級(jí)服務(wù)器是指服務(wù)器剔除了一些功能,這樣服務(wù)器在部署,運(yùn)行時(shí)開銷就會(huì)降低,變得很輕。
這里的micro web 服務(wù)器是指flask為了保持core簡(jiǎn)單,但是功能是可以擴(kuò)展的。也就是說(shuō)flask只提供了核心的web server 功能。
flask沒有提供database abstraction layer, form validation, upload handling等其它功能,但是卻提供了擴(kuò)展來(lái)支持這些功能。

成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的東明網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!1 開始Hello, World! code

from flask import Flask

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

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

run server

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

browser

http://127.0.0.1:5000/

2 Routing

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

3 Variable Rules

位URL指定參數(shù),可選地還可以指定類型。最后傳給函數(shù)。

@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,)

案例:@app.route('/user/<username>')
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,)
報(bào)錯(cuò):
AssertionError: View function mapping is overwriting an existing endpoint function: show_user_profile
原因:
兩個(gè)URL綁定到一個(gè)view function, flask認(rèn)為我們隊(duì)view function進(jìn)行了overwriting

4 Unique URLs / Redirection Behavior

@app.route('/projects/')
如果請(qǐng)求URL為/projects,這是會(huì)redirect到/projects/
請(qǐng)求會(huì)響應(yīng)一個(gè)308,然后再響應(yīng)一個(gè)200

note: URL 匹配規(guī)則為由上向下

案例:@app.route('/post/<int:post_id>/')
br/>@app.route('/post/<int: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,)

結(jié)果:
不管請(qǐng)求是post/2 還是post/2/,只有show_post響應(yīng)。

方案: 調(diào)換兩個(gè)view function的順序。

5 URL Building

建立一個(gè)對(duì)應(yīng)function的URL
用法:
第一個(gè)參數(shù): function name
后面的key arguments對(duì)應(yīng)URL variables
未知的arguments對(duì)應(yīng)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

處理不同的請(qǐng)求,需要為route()指定methods參數(shù)

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

7 Rendering Templates

使用render_template函數(shù)render a template
第一個(gè)參數(shù):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 %>
<h2> Hello {{ user }}!</h2>
<% else %>
<h2> index page!</h2>
<% 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:

標(biāo)題名稱:一flask介紹-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://muchs.cn/article32/ddpgsc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、網(wǎng)站設(shè)計(jì)公司、做網(wǎng)站、網(wǎng)站策劃、微信公眾號(hào)自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)