Question

I have a view in a Blueprint:

@blueprint1.route("/home"):
   #load some data
   ....
   #if certain condition is present, I want to essentially forward the request
   if data.has_condition:
       return render_template(????)

Or alternatively, I want to forward the request to a different blueprint for processing. Is there anyway to accomplish this?

Was it helpful?

Solution

As route handlers are just Python functions, there is no reason you can't do this:

# app/bp_zero.py
blueprint_zero = Blueprint(... etc. ...)

@blueprint_zero.route("/some-route")
def handle_some_route():
    return "Hello from some-route"

# app/bp_one.py
from .bp_zero import handle_some_route

@blueprint1.route("/home")
def handle_some_route():
    if some_condition:
        return "Hello from home"
    else:
        return handle_some_route()

Alternatively, if you just want to use a template that you have defined in one blueprint's template folder in another blueprint, you can just reference it by name:

return render_template("some/blueprint_zero/template.html")

Flask dumps all the templates for all the blueprints into one global namespace (which is why blueprints are advised to "namespace" their templates by putting them in a sub-folder).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top