28
loading...
This website collects cookies to deliver better user experience
> pipenv install Flask
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
flask run
的時候知道要去跑這支腳本:(flask-app) > export FLASK_APP=hello.py
(flask-app) > flask run
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
(flask-app)
表示該 shell 是位於 Python 虛擬環境內。(flask-app) > export FLASK_ENV=development
(flask-app) > flask run
@app.route('/')
def index():
return 'Index Page'
@app.route('/hello')
def hello():
return 'Hello, World!'
@app.route('/user/<username>')
def show_user_profile(username):
# show the user profile for that user
return f'User {username}'
@app.route('/post/<int:post_id>')
def show_post(post_id):
# show the post with the given id, the id is an integer
return f'Post {post_id}'
@app.route('/path/<path:subpath>')
def show_subpath(subpath):
# show the subpath after /path/
return f'Subpath {subpath}'
app.route()
內的字串如果用角括號包住(像這樣: <variable_name>
)會被識別成變數,且不僅 app.route()
可以識別這樣的變數,後續的函式也一樣可以識別這樣的變數。<int: post_id>
就是把客戶端請求的字串轉換成整數型態;<path: subpath>
就是把客戶端請求的字串轉換成路徑型態,完整的轉型清單如下:@app.route('/projects/')
def projects():
return 'The project page'
@app.route('/about')
def about():
return 'The about page'
route()
加上 methods 參數即可(注意引數名稱是複數的 methods,別拼錯了):from flask import request
app = Flask(__name__)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
return do_the_login()
else:
return show_the_login_form()
url_for()
就可以在 Python 腳本內自動產生這些資源檔的 URL:url_for('static', filename='style.css')
render_template()
方法來使用模板:from flask import render_template
@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
return render_template('hello.html', name=name)
/application.py
/templates
/hello.html
/application
/__init__.py
/templates
/hello.html
<!doctype html>
<title>Hello from Flask</title>
{% if name %}
<h1>Hello {{ name }}!</h1>
{% else %}
<h1>Hello, World!</h1>
{% endif %}
request
、session
、g
物件與 get_flashed_messages()
方法。request
這個全域變數內,web app 利用 request
提供的資訊與客戶端互動。request
的 method
屬性內,而 form 就存放在 request
的 form
屬性內。示例:from flask import request
from flask import render_template
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if valid_login(request.form['username'], request.form['password']):
return log_the_user_in(request.form['username'])
else:
error = 'Invalid username / password'
# the code below is executed if the request method was GET or the credentials were invalid
return render_template('login.html', error=error)
form
屬性不存在,伺服器端會引發 KeyError
錯誤,客戶端則會收到 HTTP 400 錯誤。URL
參數,則使用 args
屬性內的 get()
方法:searchword = request.args.get('key', '')
enctype="multipart/form-data"
,瀏覽器才會正確的把檔案上傳。調用 request
的 files
屬性就可以調用到檔案:from flask import request
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
f = request.files['the_file']
f.save('/var/www/uploads/uploaded_file.txt')
files
本身是個 dictionary,所以必須呼叫它裡面的 key 才會接到真正的檔案,這個 file
物件的行為就像標準的 Python file
物件,但它有個 save()
方法讓我們把檔案存到自己想要的路徑。filename
屬性,但由於檔名的不可預知,可能會有安全風險,最好用 Werkzeug 的 secure_filename()
方法過濾掉:from flask import request
from werkzeug.utils import secure_filename
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
f = request.files['the_file']
f.save('/var/www/uploads/' + secure_filename(f.filename))
request
物件的 cookies
屬性,如果是要設置 cookie 就用 response
物件的 set_cookie()
方法。request
的 cookies
也是一個 dictionary,裡面放了所有可以調用的 cookie。from flask import request
@app.route('/')
def index():
username = request.cookies.get('username')
# use cookies.get(key) instead of cookies[key] to not get a KeyError if the cookie is missing.
@app.route('/')
def index():
resp = make_response(render_template(...))
resp.set_cookie('username', 'the username')
return resp
redirect()
方法,要中斷並報錯用 abort()
方法:from flask import abort, redirect, url_for
@app.route('/')
def index():
return redirect(url_for('login'))
@app.route('/login')
def login():
abort(401)
this_is_never_executed()
errorhandler()
修飾子來做客製:from flask import render_template
@app.errorhandler(404)
def page_not_found(error):
return render_template('page_not_found.html'), 404
return
那行最後面的 404
,雖然上面的修飾子已經是 404,但 return
後面還是要加 404
Flask 才認得這是 404 錯誤頁。response
物件,如果原本是回傳字串,則字串內容會被包成 response body,再加上 200 的 HTTP 狀態碼,與 text/html
的 mimetype
。response
物件,則就原樣回傳,不經過轉換加工。response
物件,加上預設參數。response
物件:(response, status, headers)
或 (response, headers)
裡面的 status
或 headers
值會變成 response
物件的屬性,其中 header
是一個 list 或 dictionary,裡面的欄位就會是 HTTP header 的資訊。response
物件。response
物件,可以使用 make_response()
方法。@app.errorhandler(404)
def not_found(error):
return render_template('error.html'), 404
make_response()
來幫它加工加上一組 header 資訊:@app.errorhandler(404)
def not_found(error):
resp = make_response(render_template('error.html'), 404)
resp.headers['X-Something'] = 'A value'
return resp
from flask import Flask, session, redirect, url_for, escape, request
from werkzeug.utils import secure_filename
app = Flask(__name__)
# Set the secret key to some random bytes. Keep this really secret!
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
@app.route('/')
def index():
if 'username' in session:
return f"Logged in as {escape(session['username'])}"
return 'You are not logged in'
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
session['username'] = request.form['username']
return redirect(url_for('index'))
return '''
<form method="post">
<p><input type=text name=username>
<p><input type=submit value=Login>
</form>
'''
@app.route('/logout')
def logout():
# remove the username from the session if it's there
session.pop('username', None)
return redirect(url_for('index'))
escape()
方法來濾掉 HTML 碼。request
使用 flash()
方法設定訊息,在下一個 request
就可以用 get_flashed_messages()
方法來讀出訊息。logger
模組,可以簡單調用:app.logger.debug('A value for debugging')
app.logger.warning('A warning occurred (%d apples)', 42)
app.logger.error('An error occurred')