Flask 应用的文件结构
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
作為微框架,即便只有一個(gè)文件也可以編寫(xiě)基于 Flask 的 Web 應(yīng)用。然而對(duì)于許多現(xiàn)實(shí)世界中的應(yīng)用,擁有數(shù)十個(gè)以上的視圖(view)是非常正常的,這時(shí)候,Flask 建議使用多個(gè) Python 模塊來(lái)組織視圖。例如:
| / yourapplication ???? / yourapplication ???????? / __init__.py ???????? / views ???????????? __init__.py ???????????? admin.py ???????????? frontend.py ???????? / static ???????????? / style.css ???????? / templates ???????????? layout.html ???????????? index.html ???????????? login.html ???????????? ... |
視圖保存在包yourapplication.views中。這里只需要放置一個(gè)空白的__init__.py文件即可。我們來(lái)看看包中的admin.py文件。首先,我們使用 Python 模塊名稱創(chuàng)建一個(gè) Flask 模塊(flask.Module)對(duì)象,這個(gè)對(duì)象行為上非常類似 flask.Flask 對(duì)象,它們大多數(shù)方法都是一樣的。下面是一個(gè)易于理解的例子:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | from flask import Module admin = Module(__name__) @admin .route( '/' ) def index(): ???? pass @admin .route( '/login' ) def login(): ???? pass @admin .route( '/logout' ) def logout(): ???? pass |
對(duì)于frontend.py我們也可以做類似的處理,接下來(lái),我們只需要確保在整個(gè)應(yīng)用程序的__init__.py中注冊(cè)這些模塊即可:
| 1 2 3 4 5 6 7 | from flask import Flask from yourapplication.views.admin import admin from yourapplication.views.frontend import frontend app = Flask(__name__) app.register_module(admin, url_prefix = '/admin' ) app.register_module(frontend) |
通過(guò)將這些模塊注冊(cè)到應(yīng)用中,應(yīng)用程序的 URL 映射表就能夠適用于這些模塊中的配置了。請(qǐng)注意 admin 模塊的注冊(cè)參數(shù) url_prefix:默認(rèn)的當(dāng)我們注冊(cè)一個(gè)模塊時(shí),缺省的 endpoint 是“/”,要使用其它的前綴,必須通過(guò) url_prefix 參數(shù)配置。
使用 Flask 模塊和直接使用 Flask 對(duì)象有什么區(qū)別呢?最主要的區(qū)別在 URL 生成的問(wèn)題上。例如,我們經(jīng)常使用的 url_for() 函數(shù),當(dāng)直接與 Flask 對(duì)象一起工作時(shí),它的第一個(gè)參數(shù),也就是所謂的 endpoint,是視圖函數(shù)的名稱,而當(dāng)結(jié)合 Flask 模塊一起工作時(shí),對(duì)于同一個(gè)模塊中的視圖函數(shù),用法仍然一樣,而對(duì)于別的模塊中的函數(shù),則需要使用模塊名加上句點(diǎn)作為前綴。看看下面的例子可以幫助我們更 容易的理解這個(gè)問(wèn)題。假設(shè)我們?cè)?admin 模塊中有一個(gè)需要重定向到 frontend 模塊的函數(shù),它看起來(lái)類似這樣:
| 1 2 3 4 5 6 7 | @admin .route( '/to_frontend' ) def to_frontend(): ???? return redirect(url_for( 'frontend.index' )) @frontend .route( '/' ) def index(): ???? return "I'm the frontend index" |
而如果我們只需要重定向到相同模塊中的其它函數(shù),那么我們既可以使用完成的 endpoint 路徑,也可以只使用函數(shù)名:
| 1 2 3 4 5 6 7 | @frontend .route( '/to_index' ) def to_index(): ???? return redirect(url_for( 'index' )) @frontend .route( '/' ) def index(): ???? return "I'm the index" |
更進(jìn)一步,如果我們的 Module 對(duì)象是放在 Python 包中的,這樣,我們有增加了額外的放置模板和靜態(tài)文件的位置。假設(shè)我們的應(yīng)用程序看起來(lái)像是這樣的:
| / yourapplication ???? __init__.py ???? / apps ???????? __init__.py ???????? / frontend ???????????? __init__.py ???????????? views.py ???????????? / static ???????????????? style.css ???????????? / templates ???????????????? index.html ???????????????? about.html ???????????????? ... ???????? / admin ???????????? __init__.py ???????????? views.py ???????????? / static ???????????????? style.css ???????????? / templates ???????????????? list_items.html ???????????????? show_item.html ???????????????? ... |
這些包中的靜態(tài)目錄將會(huì)被自動(dòng)展開(kāi)為 URL。假設(shè)這個(gè)admin模塊是通過(guò) /admin 前綴展現(xiàn)在 URL 中的,那么可以通過(guò)/admin/static/style.css 來(lái)訪問(wèn)其中的樣式表文件。而該文件的 endpoint 則是 'admin.static'。
與 URL 規(guī)則可以省略前綴不同,我們總是需要使用完成的模塊名稱來(lái)引用模板,例如:render_template('admin/list_items.html') 等等。同樣的,既然我們的視圖函數(shù)已經(jīng)從yourapplication.views.admin移動(dòng)到y(tǒng)ourapplication.apps.admin.views中了,我們需要在注冊(cè)模塊的時(shí)候明確的設(shè)置一個(gè)名稱。這是由于再使用 __name__ 作為參數(shù)的話它這時(shí)候的值是 views 了:
| 1 2 | # in yourapplication/apps/admin/views.py admin = Module(__name__, 'admin' ) |
同樣的,引導(dǎo)程序也需要稍作調(diào)整:
| 1 2 3 4 5 6 7 8 | # in yourapplication/__init__.py from flask import Flask from yourapplication.apps.admin.views import admin from yourapplication.apps.frontend.views import frontend app = Flask(__name__) app.register_module(admin, url_prefix = '/admin' ) app.register_module(frontend) |
值得注意的是,如果我們使用一個(gè)不合格的 endpoint,默認(rèn)的 Flask 會(huì)將它當(dāng)作是模塊的靜態(tài)文件目錄,即便這個(gè)目錄并不存在。這對(duì)于任何 endpoint 都有效,而不僅僅是名為 static 的目錄,只不過(guò)通常我們使用 static 放置靜態(tài)文件而非設(shè)置一個(gè)視圖函數(shù)而已。如果需要使用整個(gè)應(yīng)用程序的靜態(tài)目錄,可以在最開(kāi)始加上一個(gè)句點(diǎn):
| 1 2 3 4 5 | # this refers to the application's static folder url_for( '.static' , filename = 'static.css' ) # this refers to the current module's static folder url_for( 'static' , filename = 'static.css' ) |
轉(zhuǎn)載于:https://my.oschina.net/935572630/blog/371449
總結(jié)
以上是生活随笔為你收集整理的Flask 应用的文件结构的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: MSP430F5529 DriverLi
- 下一篇: 算法学习:蝙蝠算法简介