python-web框架flask:快速搭建

參考:快速上手

python-web框架flask:搭建一個完整程序 配合服用,效果更佳。

1、flask全局應用對象

1、創建一個flask全局應用對象。

	
	app = Flask(__name__)
	

       創建一個全局對象非常簡單,name 指定這個全局對象的 應用模塊或者包的名稱。
       我們可以點開Flask這個方法,裏面可以配置一些參數,我們重點講幾個參數的意思。也是正常我們會用到的:

	 def __init__(
	        self,
	        import_name,
	        static_url_path=None,
	        static_folder="static",
	        static_host=None,
	        host_matching=False,
	        subdomain_matching=False,
	        template_folder="templates",
	        instance_path=None,
	        instance_relative_config=False,
	        root_path=None,
	    ):

       看一下static_folder,這個參數,默認是static,這個參數是我們加載靜態資源的相對路徑, 有時候我們的目錄機構可能不叫static,這個時候如果還是使用默認配置就找不到我們的靜態資源了,不要慌,我們只需要修改這個路徑既可。

       然後還有一個同樣配置的參數我們需要看一下就是 template_folder,這個是配置我們的html模板的相對地址,和上面的static的配置類似。

注意:這邊的相對路徑是相對於當前的啓動的py文件,比如說當前的目錄結構是這樣的:
在這裏插入圖片描述
       那麼我們的static_folder就應該配置爲 . ./static,因爲這個路徑是相對於啓動函數

2、啓動和端口監聽

       我們的程序既然有了全局應用對象,那麼怎麼去啓動呢,有兩種方式,如果在我們的ide:pyCharm中啓動,我們可以用run方法來實現:

	 app.run(
	        host='127.0.0.1',
	        port=8082,
	        debug=True
	    )

非常簡單,我們就可以啓動一個應用,當然這只是最簡單的,還有很多拓展工作沒做

在shell機器下面我們的啓動比較麻煩,命令如下:

	$env:FLASK_APP = "main.py"	

       因爲沒有真實嘗試過,所以具體還是以官方文檔爲主:It`s 官方文檔-部署


2、路由

1、靜態路由

       正常情況下,像我們的歡迎頁之類的都是靜態路由,不攜帶動態參數,我們來看一下他的書寫方式(注:app是我們的flask全局應用對象),這邊採用route方法:

	@app.route('/')
	def index():
    return 'Index Page'

2、帶參數路由

       如果我們的一個路徑需要攜帶參數怎麼辦呢,不要慌,flask擁有對應的參數匹配規則

       這邊的參數傳遞有兩種方式,一種是原始的參數傳遞<variable_name>,一種是參數轉換傳遞<converter:variable_name>,而方法在接收到參數的時候,可以直接進行使用,看一下這幾個路由:

	@app.route('/user/<username>')
	def show_user_profile(username):
	    # show the user profile for that user
	    return 'User %s' % escape(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 'Post %d' % post_id
	
	@app.route('/path/<path:subpath>')
	def show_subpath(subpath):
	    # show the subpath after /path/
	    return 'Subpath %s' % escape(subpath)

       第一個是普通的參數傳遞,可以看到我們在函數入參裏面直接使用,第二三個是轉換後的參數,對應的轉換規則如下:

轉換關鍵字 作用
string (缺省值) 接受任何不包含斜槓的文本
int 接受正整數
float 接受正浮點數
path 類似 string ,但可以包含斜槓
uuid 接受 UUID 字符串

3、路由重定向

       其實我感覺這個沒啥可說的,但是可能在開發的過程中會誤寫一些東西,導致我們的程序運行出錯,所以還是這邊解釋一下,看一下下面兩個路徑,不同之處在於是否使用尾部的斜槓:

	@app.route('/projects/')
	def projects():
	    return 'The project page'
	
	@app.route('/about')
	def about():
	    return 'The about page'

       第一個路徑的寫法,當你訪問一個沒有斜槓結尾的 URL 時 Flask 會自動進行重定向,幫你在尾部加上一個斜槓。
       第二個路徑的寫法,如果訪問這個 URL 時添加了尾部斜槓就會得到一個 404 錯誤。
       官方的說法是:這樣可以保持 URL 唯一,並幫助搜索引擎避免重複索引同一頁面。

4、構建URL

1、模擬請求

       url_for() 函數用於構建指定函數的 URL,我的理解就是說,通過這個函數可以模擬一個對應的http請求,而且可以加上相應的入參等,我們來看一下下面的代碼和輸出:

	
	app = Flask(__name__)
	
	@app.route('/')
	def index():
	    return 'index'
	
	@app.route('/login')
	def login():
	    return 'login'
	
	@app.route('/user/<username>')
	def profile(username):
	    return '{}\'s profile'.format(escape(username))
	
	# 全局應用對象自帶方法
	with app.test_request_context():
	  😂  print(url_for('index'))
	  😘  print(url_for('login'))
	  😍  print(url_for('login', next='/'))
	  😊  print(url_for('profile', username='John Doe'))

輸出:

	😂	/
	😘	/login
	😍	/login?next=/
	😊	/user/John%20Doe

2、訪問靜態文件

       理想情況下你的服務器已經配置好了爲你的提供靜態文件的服務。只要在你的包或模塊旁邊創建一個名爲 static 的文件夾就行了。 靜態文件位於應用的 /static 中。

使用特定的 ‘static’ 端點就可以生成相應的 URL:

	url_for('static', filename='style.css')

       這個靜態文件在文件系統中的位置應該是 static/style.css

5、Http請求方法

       可以使用 route() 裝飾器的 methods 參數來處理不同的 HTTP 方法,如下:

	from flask import request
	
	@app.route('/login', methods=['GET', 'POST'])
	def login():
	    if request.method == 'POST':
	        return do_the_login()
	    else:
	        return show_the_login_form()

       如果當前使用了 GET 方法, Flask 會自動添加 HEAD 方法支持,並且同時還會 按照 HTTP RFC 來處理 HEAD 請求。同樣, OPTIONS 也會自動實現。


3、渲染模板

       使用 render_template() 方法可以渲染模板,你只要提供模板名稱和需要 作爲參數傳遞給模板的變量就行了。Flask 自動爲你配置 Jinja2 模板引擎。
       Flask 會在 templates 文件夾內尋找模板。 因此你的模板和應用會有兩種體現方式:

1、應用是一個模塊

       如果應用是一個模塊的話,那麼可能存在多個模塊和模板對應,所以模板作爲單獨一個包實現,目錄結構如下:

	/application.py
	/templates
		 /我的模板.html

2、應用只有一個包

       如果你的應用知識一個包的話,那麼可以直接把模板放在包下面,但是記住要加上templates的外層包:

   /application
	  /__init__.py
	  /templates
 			   /我的模板.html

       我感覺就是其實沒有那麼多死板的規定,你只要記住Jinja2 模板引擎 可以查找templates包下面的模板,剩下的就是自我發揮了

3、模板渲染

看一下一個小例子:

	from flask import render_template

	@app.route('/hello/')
	@app.route('/hello/<name>')
	def hello(name=None):
	    return render_template('hello.html', name=name)
	<!doctype html>
	
	<title>Hello from Flask</title>
	{% if name %}
	  <h1>Hello {{ name }}!</h1>
	{% else %}
	  <h1>Hello, World!</h1>
	{% endif %}

       模板可以繼承使用,比如說一些公共的部分,可以參考:模板繼承

       自動轉義默認開啓。因此,如果 name 包含 HTML ,那麼會被自動轉義。如果你可以 信任某個變量,且知道它是安全的 HTML (例如變量來自一個把 wiki 標記轉換爲 HTML 的模塊),那麼可以使用 Markup 類把它標記爲安全的,或者在模板 中使用 |safe 過濾器。這個具體可以參考:Jinja2模板


4、請求數據

1、請求對象

  • 通過使用 method 屬性可以操作當前請求方法
  • 通過使用 form 屬性處理表單數據(在 POST 或者 PUT 請求 中傳輸的數據)
  • 要操作 URL (如 ?key=value )中提交的參數可以使用 args 屬性

我們來看一下例子:

	@app.route('/login', methods=['POST', 'GET'])
	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'
		
		searchword = request.args.get('key', '')
		print(searchword)
			
	    # 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 。 如果不像捕捉一個標準錯誤一樣捕捉 KeyError ,那麼會顯示一個 HTTP 400 Bad Request 錯誤頁面。

2、文件上傳

       文件上傳的前提是: 在HTML 表單中設置 enctype=“multipart/form-data” 屬性

       已上傳的文件被儲存在內存或文件系統的臨時位置。

操作方式如下:

  • 通過請求對象 files 屬性來訪問上傳的文件。
  • save() 方法用於把上傳文件保存到服務器的文件系統中的

看一下例子(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))

3、Cookies

       要訪問 cookies ,可以使用 cookies 屬性。可以使用響應 對象 的 set_cookie 方法來設置 cookies 。請求對象的 cookies 屬性是一個包含了客戶端傳輸的所有 cookies 的字典。

在 Flask 中,如果使用 會話 ,那麼就不要直接使用 cookies ,因爲 會話 比較安全一些。

讀取 cookies:

	from flask import request

	@app.route('/')
	def index():
	    username = request.cookies.get('username')

儲存 cookies:

	from flask import make_response
	
	@app.route('/')
	def index():
	    resp = make_response(render_template(...))
	    resp.set_cookie('username', 'the username')
	    return resp

5、響應

1、重定向

使用 redirect() 函數可以重定向。

	from flask import abort, redirect, url_for

	@app.route('/')
	def index():
    	return redirect(url_for('login'))
	
	@app.route('/login')
	def login():
		return “”

2、錯誤(狀態碼)處理

使用 errorhandler() 裝飾器可以定製出錯頁面:

	from flask import render_template

	@app.errorhandler(404)
	def page_not_found(error):
	    return render_template('page_not_found.html'), 404

json格式化處理

       JSON 格式的響應是常見的,用 Flask 寫這樣的 API 是很容易上手的。如果從視圖 返回一個 dict (鍵值對形式),那麼它會被轉換爲一個 JSON 響應。

	@app.route("/me")
	def me_api():
	    user = get_current_user()
	    return {
	        "username": user.username,
	        "theme": user.theme,
	        "image": url_for("user_image", filename=user.image),
	    }

       如果 dict 還不能滿足需求,還需要創建其他類型的 JSON 格式響應,可以使用 jsonify() 函數。該函數會序列化任何支持的 JSON 數據類型。

	@app.route("/users")
	def users_api():
	    users = get_all_users()
	    return jsonify([user.to_json() for user in users])

6、會話

1、利用flask生成祕匙

       生成隨機數的關鍵在於一個好的隨機種子,因此一個好的密鑰應當有足夠的隨機性。

        使用下面的命令 可以快捷的爲 Flask.secret_key ( 或者 SECRET_KEY )生成值:

	$ python -c 'import os; print(os.urandom(16))'
	
	b'_5#y2L"F4Q8z\n\xec]/'

2、會話處理

        session對象:允許你在不同請求 之間儲存信息。這個對象相當於用密鑰簽名加密的 cookie ,即用戶可以查看你的 cookie ,但是如果沒有密鑰就無法修改它。

使用示例:

	from flask import Flask, session, redirect, url_for, escape, request

	app = Flask(__name__)
	
	# Set the secret key to some random bytes. Keep this really secret!
	app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
	
	# 獲取session的key對應的值
	@app.route('/')
	def index():
	    if 'username' in session:
	        return 'Logged in as %s' % escape(session['username'])
	    return 'You are not logged in'
	
	# session的存儲
	@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>
	    '''
	
	# session移除某個key
	@app.route('/logout')
	def logout():
	    # remove the username from the session if it's there
	    session.pop('username', None)
	    return redirect(url_for('index'))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章