標籤:redirect pycharm collision val 參考 als com deferred 表示
主旨:
主要記錄在使用flask buleprint的時候遇到的一些問題。
問題1:
在建立藍圖的時候比如我在A/keystone.py建立一個藍圖
a_keystone = Blueprint('keystone',__name__),沒毛病。
當我在B/keystone.py建立一個藍圖的時候b_keystone = Blueprint('keystone',__name__)。也沒毛病哈,但是當我啟動服務的時候就會報錯,說是註冊藍圖衝突。報錯如下:
(blueprint, self.blueprints[blueprint.name], blueprint.name)AssertionError: A blueprint's name collision occurred between <flask.blueprints.Blueprint object at 0x10e090bd0> and <flask.blueprints.Blueprint object at 0x10e090450>. Both share the same name "keystone". Blueprints that are created on the fly need unique names.
我們先看一下flask相關的源碼:
# blueprints.pyclass Blueprint(_PackageBoundObject): # indicator. warn_on_modifications = False _got_registered_once = False def __init__(self, name, import_name, static_folder=None, static_url_path=None, template_folder=None, url_prefix=None, subdomain=None, url_defaults=None, root_path=None): _PackageBoundObject.__init__(self, import_name, template_folder, root_path=root_path) self.name = name self.url_prefix = url_prefix self.subdomain = subdomain self.static_folder = static_folder self.static_url_path = static_url_path self.deferred_functions = [] if url_defaults is None: url_defaults = {} self.url_values_defaults = url_defaults
繼承_PackageBoundObject的Blueprint類的在初始化函數中,初始化了很多參數:name, url_prefix, subdomain, static_folder...,這些參數具體是什麼意思呢,我們還是以例子為基準,沒有涉及到的參數(即設定為None)暫時不管. 在官方文檔給出的My First Buleprint例子中:
simple_page = Blueprint('simple_page', __name__, template_folder='templates')
傳入的參數有: self.name = 'simple_page', import_name = name, template_folder = 'templates'. 在我們的例子中name即是 'blueprintDemo'(ps:python中__name__預設為所在檔案名稱)
注意:中間省略一些內容,詳情參考http://www.jianshu.com/p/a55b9319a3e0
當我們註冊藍圖
app.register_blueprint(simple_page, url_prefix='/page')
重點來啦!
@setupmethod def register_blueprint(self, blueprint, **options): # indicator. first_registration = False if blueprint.name in self.blueprints: assert self.blueprints[blueprint.name] is blueprint, 'A blueprint\'s name collision occurred between %r and ' '%r. Both share the same name "%s". Blueprints that ' 'are created on the fly need unique names.' % (blueprint, self.blueprints[blueprint.name], blueprint.name) else: # 添加藍到self.blueprints. self.blueprints[blueprint.name] = blueprint self._blueprint_order.append(blueprint) # 設定為True. first_registration = True blueprint.register(self, options, first_registration)
這個時候我們有一個疑問在通過url_for('藍圖名.函數名')進行頁面跳轉的時候(注意,不全是頁面跳轉,取決於函數裡的邏輯,我這裡通過頁面跳轉驗證。)這個藍圖名怎麼理解?比如xxx = Blueprint('###',__name__,url_prefix='/a',template_folder='./template-test')是xxx還是###(xxx和###表示可以起合法的任意變數,由於不確定暫用特殊來代替)
驗證項目請見github:https://github.com/jasonhubs/flask_test
通過驗證得知:Blueprint()裡面的參數有個為藍圖名,這個很重要,而且在所有藍圖當中必須要唯一標誌,因為在template中通過url_for('藍圖名.函數名')跳轉頁面的時候很重要,至於xxx = Blueprint(...)xxx主要作用就是為了註冊的時候用
意外發現
在flask template中注釋的部分依然不起作用,如下:
<!--<a href="url_for('bp.user')">this will redirect to user.html!</a>-->,在這行代碼中如果url_for裡面的錯誤,依然還是會報錯的,我很納悶,為啥注釋了,還起作用。我用的IDE為pycharm。這個暫時沒解決。解決了我會及時更新文檔。有知道的朋友也告訴我一下,謝謝。
flask之blueprint和url_for的關係