diff --git a/CHANGES.rst b/CHANGES.rst index b006b732..1dbb9605 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -38,6 +38,8 @@ Unreleased dependency to >= 0.15. :issue:`3022` - Support ``static_url_path`` that ends with a forward slash. :issue:`3134` +- Support empty ``static_folder`` without requiring setting an empty + ``static_url_path`` as well. :pr:`3124` - :meth:`jsonify` supports :class:`dataclasses.dataclass` objects. :pr:`3195` - Allow customizing the :attr:`Flask.url_map_class` used for routing. diff --git a/flask/app.py b/flask/app.py index 7a2c7dc4..76e45a30 100644 --- a/flask/app.py +++ b/flask/app.py @@ -408,11 +408,8 @@ class Flask(_PackageBoundObject): self, import_name, template_folder=template_folder, root_path=root_path ) - if static_url_path is not None: - self.static_url_path = static_url_path - - if static_folder is not None: - self.static_folder = static_folder + self.static_url_path = static_url_path + self.static_folder = static_folder if instance_path is None: instance_path = self.auto_find_instance_path() @@ -594,7 +591,7 @@ class Flask(_PackageBoundObject): bool(static_host) == host_matching ), "Invalid static_host/host_matching combination" self.add_url_rule( - self.static_url_path.rstrip("/") + "/", + self.static_url_path + "/", endpoint="static", host=static_host, view_func=self.send_static_file, diff --git a/flask/blueprints.py b/flask/blueprints.py index eb3ce7aa..1e77be9e 100644 --- a/flask/blueprints.py +++ b/flask/blueprints.py @@ -208,7 +208,7 @@ class Blueprint(_PackageBoundObject): if self.has_static_folder: state.add_url_rule( - self.static_url_path.rstrip("/") + "/", + self.static_url_path + "/", view_func=self.send_static_file, endpoint="static", ) diff --git a/flask/helpers.py b/flask/helpers.py index 9825202a..582a2b87 100644 --- a/flask/helpers.py +++ b/flask/helpers.py @@ -965,24 +965,27 @@ class _PackageBoundObject(object): ) del _get_static_folder, _set_static_folder - def _get_static_url_path(self): + @property + def static_url_path(self): + """The URL prefix that the static route will be accessible from. + + If it was not configured during init, it is derived from + :attr:`static_folder`. + """ if self._static_url_path is not None: return self._static_url_path if self.static_folder is not None: basename = os.path.basename(self.static_folder) - return "/" + basename if basename else "" + return ("/" + basename).rstrip("/") + + @static_url_path.setter + def static_url_path(self, value): + if value is not None: + value = value.rstrip("/") - def _set_static_url_path(self, value): self._static_url_path = value - static_url_path = property( - _get_static_url_path, - _set_static_url_path, - doc="The URL prefix that the static route will be registered for.", - ) - del _get_static_url_path, _set_static_url_path - @property def has_static_folder(self): """This is ``True`` if the package bound object's container has a diff --git a/tests/test_basic.py b/tests/test_basic.py index 025b5a99..b7eeeb40 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -1427,14 +1427,14 @@ def test_static_url_path_with_ending_slash(): assert flask.url_for("static", filename="index.html") == "/foo/index.html" -def test_static_url_null_path(app): +def test_static_url_empty_path(app): app = flask.Flask(__name__, static_folder='', static_url_path='') rv = app.test_client().open('/static/index.html', method='GET') assert rv.status_code == 200 rv.close() -def test_static_url_null_path_defaulting(app): +def test_static_url_empty_path_default(app): app = flask.Flask(__name__, static_folder='') rv = app.test_client().open('/static/index.html', method='GET') assert rv.status_code == 200