I wanted to include a commit date and hash in my application.
For this I generated the commit date and hash, when deploying, as static file.
The static files then got pushed to my configured storage via collectstatic
.
This static files were then included via template tags in my templates.
For this I needed to use the static_storage.open
with the relative path (of the apps static directory).
# Original files are at app_name/static/app_name/commit.txt
handle = static_storage.open('app_name/commit.txt')
At first I struggled because I though I can use url
or path
from the static_storage
module, but these methods only give you the valid url.
Accessing the contents of files from these urls won’t work with the std lib open
methods.
As a bonus, I only showed them in the template when DEBUG was True.