Issue
Reading about the url_for
function I have been quite confused about what is the meaning of the configuration values mentioned in the question. My assumption is as follows:
BASE_URL = PREFERRED_URL_SCHEME + SERVER_NAME + APPLICATION_ROOT
Now, I have the following questions:
- Is the assumption conceptually valid?
- If yes to the previous question, is
BASE_URL
used by Flask in that way?
Edit: since someone voted to close this question due to lack of clarity, I will explain a bit more about the nature of the question:
The configuration values mentioned in the question are in multiple parts of the Flask documentation, however, so far I have not been able to find a clear definition of these and with examples, other than vague mentions in forums. Now, resolving the assumption I raised above might give more clarity on the representation of those values, an assumption that was born out of this piece of documentation:
base_url – Base URL where the app is being served, which path is relative to. If not given, built from PREFERRED_URL_SCHEME, subdomain, SERVER_NAME, and APPLICATION_ROOT.
Now, I know that the existence of all those configuration values is because some users might prefer to specify some and let Flask infer the others, so it would be very useful to have an example that gives clarity on how to assign those values (especially if there is a need to use all of them). For example, if we have the following:
PREFERRED_URL_SCHEME = https
SERVER_NAME = www.mywebsite.com
APPLICATION_ROOT = /united-states
A user will know that he has to set the following configuration value (in case there is a need to use it):
BASE_URL = https://www.mywebsite.com/united-states
Solution
I'm not a Flask expert, I have only built two or three non-trivial apps. But there are still no answers yet, let me try to address some of your questions.
AFAIK, the only important setting is the APPLICATION_ROOT
. It must match the http server's config or else the app will not work properly. In regular deployment the Flask will receive incoming requests containing the URL scheme and the server name, you don't have to put these into the app config.
The docs confirms that. "PREFERRED_URL_SCHEME
Use this scheme for generating external URLs when not in a request context." But except in testing, the flask functions are in a request context.
The situation with the SERVER_NAME
is little bit more complicated, it is related to subdomain matching, I'm not really familiar with. There is some information (with many upvotes!) here: Flask app that routes based on subdomain
The linked docs for test_request_context
also says: "This is mostly useful during testing, where you may want to run a function that uses request data without dispatching a full request." It seems logical that in case of non-existing real request the missing values must be given by other means.
Finally, the code (testing.py
file) answers your question how the base_url
is being built from the pieces:
def __init__(
self,
app: "Flask",
path: str = "/",
base_url: t.Optional[str] = None,
subdomain: t.Optional[str] = None,
url_scheme: t.Optional[str] = None,
*args: t.Any,
**kwargs: t.Any,
) -> None:
assert not (base_url or subdomain or url_scheme) or (
base_url is not None
) != bool(
subdomain or url_scheme
), 'Cannot pass "subdomain" or "url_scheme" with "base_url".'
if base_url is None:
http_host = app.config.get("SERVER_NAME") or "localhost"
app_root = app.config["APPLICATION_ROOT"]
if subdomain:
http_host = f"{subdomain}.{http_host}"
if url_scheme is None:
url_scheme = app.config["PREFERRED_URL_SCHEME"]
url = url_parse(path)
base_url = (
f"{url.scheme or url_scheme}://{url.netloc or http_host}"
f"/{app_root.lstrip('/')}"
)
Answered By - VPfB
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.