Neither Django, nor any maintained Python framework has an automatic match between file paths on the server and Urls - this is something of static HTML that comes from the pre-1.0 Web - and it’s something that happens in CGI applications, or in technologies like PHP or Asp.
What you need to put on href
from your page is the address for a view, like any other view in Django. The code of that view is going to return, instead of HTML, an image file, which will be marked as such in HTTP headers. (The framework does this automatically).
Within the Python code of this view, you can choose to match the path that comes in the URL directly to the arch on the disk, although this is bad security practice. The technologies that do this by default have led years old to close all (if there isn’t any left) security holes derived from this (for example, the user when placing directories under the name "." in the URL could access any file visible by the HTTP server process on the server - this trivial vulnerability persisted for years in some technologies).
Of course it is possible to put your files in the configured folder
to serve static resources - below it there is a match between the path passed in the URL and the directory structure /static/...
- and as this is done by the framework, the possible gaps are already well resolved.
But the most normal in these cases is to have an image generated by the application, of
dynamic form - be it for each access, be it a graph generated for each
time interval, etc... in this case, enters the view idea -
The function serve(request, caminho_do_arquivo)
can be used in your view to serve a file directly (import it with from django.contrib.staticfiles.views import serve
).
The file path may be associated with an image ID in the database - and never be exposed in the URL, if you prefer
In: <a href="result.download" download> read <a href="{{ result.download }}" download>
– Thauan Patrick Amaral