Comments (5)
I'm not clear what the request is here. You should already be able to do this with host_matching=True
. For the 5% that app A needs, make a blueprint only for A's domain, same for the 5% that only B needs.
from flask.
Maybe you're saying that Blueprint()
does not take a host=
parameter. But bp.register()
does take a host
parameter. So you can still apply this to all routes on a blueprint without change, you just have to specify it at registration instead of at creation.
from flask.
I want some routes to be available to example.org only, others to somesite.com only, and some to both. Didn't find any example of how to do it with Blueprints.
What's bp.register()
?
from flask.
Sorry, meant app.register_blueprint
. But you can do that. You can have any layout you want with blueprints. So make some that are shared, some that have host=first_host
, and some that have second_host
.
a_bp = Blueprint()
b_bp = Blueprint()
shared_bp = Blueprint()
app.register_blueprint(a_bp, host="a")
app.register_blueprint(b_bp, host="b")
app.register_blueprint(shared_bp)
Something along those lines. Sorry, I don't really use this feature, so I don't have a ready made environment to spin up to answer your specific question.
from flask.
i have the following example:
"""
attempt at an MVCE for Flask with domain blueprints (Flask 3.x)
python3 flaskapp-with-blueprint-example-1.py
"""
from flask import Blueprint, Flask, request
def create_app():
app = Flask(__name__)
first_bp = Blueprint('first', __name__)
second_bp = Blueprint('second', __name__)
shared_bp = Blueprint('shared', __name__)
@first_bp.route('/')
def first_home():
return f"""route meant only for first.localhost"""
@second_bp.route('/')
def second_home():
return f"""route meant only for second.localhost"""
@shared_bp.route('/shared')
def shared_page():
return f"""this route should work with both domains"""
app.register_blueprint(first_bp, host="first.localhost")
app.register_blueprint(second_bp, host="second.localhost")
app.register_blueprint(shared_bp)
return app
if __name__ == "__main__":
app = create_app()
app.run(
debug=True,
host='0.0.0.0',
port=6123,
# if running it inside of my docker container:
# ssl_context=(
# "/etc/apache2/ssl/SSLforMyHosts-certificate.pem",
# "/etc/apache2/ssl/SSLforMyHosts-key.pem",
# ),
)
"""adding these lines to your /etc/hosts file should be enough to get up and running:
127.0.0.1 first.localhost
127.0.0.1 second.localhost
"""
it runs, but not as expected:
- http://first.localhost:6123/ returns "route meant only for first.localhost" (ok)
- http://second.localhost:6123/ returns "route meant only for first.localhost" as well which is wrong. expected to get "route meant only for second.localhost".
- http://first.localhost:6123/shared returns "this route should work with both domains" (ok)
- http://second.localhost:6123/shared returns "this route should work with both domains" (ok)
i thought maybe i need this to set app
instead:
app = Flask(__name__, host_matching=True, static_host='first.localhost')
but then every route becomes 404 Not Found.
i don't understand what static_host
is. i guess it's the host used to serve static data but it's counter-intuitive that i should set it to a single value when working with multiple domains. likewise for app.config['SERVER_NAME']
(i don't understand if i'm supposed to set it or not, either way setting it to 'first.localhost' did not help in the examples above.)
no offense but the documentation could be clearer. host_matching
and domain are not mentioned once on the blueprints page (only subdomains are.)
from flask.
Related Issues (20)
- Flask 3.0.1 breaks path-separated command line arguments HOT 2
- Invalid `SERVER_NAME` + `url_for` leads to `AttributeError` HOT 3
- Issue matching route with methods=['OPTIONS'] when similar route appears later using ['GET', 'POST'] HOT 3
- use ThreadPoolExecutor and copy_current_request_context in flask will appear some bug
- `FileStorage` closed unexpectedly when using `copy_current_request_context` HOT 1
- Blueprint Dynamic URL For `string` vs. `path`
- Extend Config type HOT 1
- send_file doesn't work with objects HOT 1
- CHIPS support HOT 1
- The dependency specifiers are too relaxed HOT 3
- Route Aliases HOT 2
- Flask logging - Other libraries - FileHandler creates duplicate log entries HOT 11
- transfer-encoding: debug server behavior differs from test client HOT 1
- Keep code running after response has been sent. (To respond with HTTP requests to other servers) HOT 6
- Secret Key Rotation HOT 1
- @app.errorhandler() cannot be used in blueprint when debug=False
- Flask is not running when Debug is True on Windows HOT 1
- Starter example results in 404 error HOT 4
- How to avoid duplicate threads in debug mode
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flask.