Thrust:
The main records are some of the problems encountered when using flask buleprint.
Question 1:
When I was creating blueprints, like I was creating a blueprint in a/keystone.py.
a_keystone = Blueprint('keystone',__name__)
, no problem.
When I was creating a blueprint in b/keystone.py b_keystone = Blueprint('keystone',__name__)
. There is no problem, but when I start the service will be an error, said the registration blueprint conflict. The error is as follows:
(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.
Let's take a look at flask related source code:
# 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.sub Domain = subdomain Self.static_folder = Static_folder Self.static_url_path = Static_url_path self.def Erred_functions = [] If url_defaults is none:url_defaults = {} Self.url_values_defaults = Url_de Faults
The blueprint class that inherits _packageboundobject has initialized a number of parameters in the initialization function: Name, Url_prefix, subdomain, static_folder ..., what do these parameters mean exactly? We still use the example as a benchmark, no parameters are involved (that is, set to none) for the time being. In the My first buleprint example given in the official documentation:
simple_page = Blueprint('simple_page', __name__, template_folder='templates')
The parameters passed in are: Self.name = ' Simple_page ', import_name = name, Template_folder = ' templates '. In our example, name is ' Blueprintdemo ' (Ps:python __name__ default is the file name)
Note : Some content is omitted in the middle, details refer to Http://www.jianshu.com/p/a55b9319a3e0
When we register the blueprint
app.register_blueprint(simple_page, url_prefix='/page')
Here's the point!
@setupmethod def register_blueprint (self, Blueprint, **options): # in Dicator. 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 ' is created on the fly need unique names. '% (Blueprint, Self.blueprint S[blueprint.name], Blueprint.name) Else: # Add blue to self.blueprints. Self.blueprints[blueprint.name] = Blueprint Self._blueprint_order.append (Blueprint) # set to True. First_registration = True blueprint.register (self, Options, first_registration)
At this time we have a question in the Url_for (' Blueprint name. Function name ') for page jumps ( note , not all page jumps, depending on the logic in the function, I am here through the page jump verification.) How do you understand this blueprint name? such as xxx = Blueprint('###',__name__,url_prefix='/a',template_folder='./template-test')
xxx or # # # (XXX and # # #表示可以起合法的任意变量, because not sure to take the special to replace)
Please see github:https://github.com/jasonhubs/flask_test for verification projects.
Verify that: Blueprint () parameters have a blueprint name, this is very important, and in all the blueprint must be unique, because in the template by url_for('蓝图名.函数名')
Jumping page is very important, as for xxx = Blueprint(...)
XXX main function is to register the time with
Accidental discovery
The annotated section in the Flask template still does not work, as follows:
<!--<a href="url_for('bp.user')">this will redirect to user.html!</a>-->
, in this line of code if url_for inside errors, still will error, I am puzzled, why comment, also function. I use the IDE for Pycharm. This is not settled for the time being. Resolved I will update the document in a timely manner. Have a friend to know also tell me, thank you.
The relationship between the blueprint of flask and url_for