Skip to content

Commit 81d3a4e

Browse files
Fix static file URL generation to use relative path (#167)
* Fix static file URL generation to use relative path from template directory * Update CHANGELOG.md with new fixed entry
1 parent a0ec8a5 commit 81d3a4e

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/
1818

1919
## [Unreleased]
2020

21+
### Fixed
22+
- Fixed static file URL generation to use correct relative paths when serving component assets through Django's static file storage system.
23+
2124
## [0.13.1]
2225

2326
### Changed

src/django_bird/staticfiles.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ def template_dir(self):
104104
def url(self) -> str:
105105
static_path = finders.find(str(self.relative_path))
106106
if static_path is not None:
107-
return self.storage.url(static_path)
107+
static_relative_path = Path(static_path).relative_to(self.template_dir)
108+
return self.storage.url(str(static_relative_path))
108109
return reverse(
109110
"django_bird:asset",
110111
kwargs={

tests/test_staticfiles.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ def test_url_with_staticfiles_finder(self, templates_dir):
155155
component = Component.from_name(button.name)
156156
asset = component.get_asset(button_css.file.name)
157157

158-
assert asset.url == f"/static{button_css.file.parent}/{button_css.file.name}"
158+
assert (
159+
asset.url == f"/static/{button_css.file.parent.name}/{button_css.file.name}"
160+
)
159161

160162
def test_url_with_reverse_fallback(self, templates_dir):
161163
button = TestComponent(

0 commit comments

Comments
 (0)