Using Flask Blueprints, how to fix url_for from hacking if subdomain is specified?

Inside the bulb drawing, I have:

frontend = Blueprint('frontend', __name__) 

and the route to my index function:

 @frontend.route('/') def index(): #code 

This works fine, but I'm trying to add a subdomain to the route, for example:

 @frontend.route('/', subdomain='<var>') def index(var): 

But this breaks the application, and the browser spits out (among other things):

 werkzeug.routing.BuildError BuildError: ('frontend.index', {}, None) 

frontend.index is called in my code in several places in url_for ('frontend.index')

How can I make url_for work when I enable the subdomain? The only thing that is in the documents that I can find, and I think it might be appropriate, is under http://flask.pocoo.org/docs/api/ :

To integrate applications, Flask has a hook to intercept the assembly of the error URL through Flask.build_error_handler. The result of the url_for function in BuildError when the current application does not have a URL for a given endpoint and values. When this happens, current_app calls it build_error_handler if it is not None, which can return a string to use as the result of url_for (instead of using the url_for BuildError exception by default) or re-create the exception. Example:

 def external_url_handler(error, endpoint, **values): "Looks up an external URL when `url_for` cannot build a URL." # This is an example of hooking the build_error_handler. # Here, lookup_url is some utility function you've built # which looks up the endpoint in some external URL registry. url = lookup_url(endpoint, **values) if url is None: # External lookup did not have a URL. # Re-raise the BuildError, in context of original traceback. exc_type, exc_value, tb = sys.exc_info() if exc_value is error: raise exc_type, exc_value, tb else: raise error # url_for will use this result, instead of raising BuildError. return url app.build_error_handler = external_url_handler 

However, I am new to python (and programming) and cannot figure out where I would put this code, or how to get this function to call when a builderror occurs.

Any insight would be appreciated :)

+8
python flask routes
source share
3 answers

First, to use subdomains, you need to have a value for the SERVER_NAME configuration:

 app.config['SERVER_NAME'] = 'example.net' 

You have a view:

 frontend = Blueprint('frontend', __name__) @frontend.route('/', subdomain='<var>') def index(var): return ... 

To recover the URL of this view, Flask requires a value for var. url_for('frontend.index') will fail, as it does not have sufficient values. With the SERVER_NAME above, url_for('frontend.index', var='foo') will return http://foo.example.net/ .

+13
source share

Add a plan name to url_for . Example:

 url_for('pay_sermepa.sermepa_cancel', _external=True) 
  • pay_sermepa : project name
  • sermepa_cancel : route
+9
source share

I do not think this is a problem with Flask.

You provide two functions with the same method name:

 @frontend.route('/') def index(): #code 

and

 @frontend.route('/', subdomain='<var>') def index(var): 

They are wrapped differently, but when flask.build_url is called, it throws itself due to an overloaded function name. At first glance, this seems wrong.

Try specifying a different function name for the second, for example

 @frontend.route('/', subdomain='<var>') def index_var(var): 

This may solve your problem. I have not tested it though.

-3
source share

All Articles