Show external folder html content in django

Hi everyone, I’m developing my first django project and need some help.

I’d like to display html content (bandwithd reports), that is in the folder /var/lib/bandwidthd/htdocs/.
There is no subdirectories, only html and png files.

I could also serve the content using the webserver (nginx) but I’d like to show the content only to the users logged in django.

If I put “/var/lib/bandwidthd/htdocs/” in TEMPLATES → DIRS (settings.py), I’m able to access for example the index.html but it has no access to the png files and to the other html files.

Is it possible to achieve, what I’d like? and if yes, how?

Thanks in advance for your help.

supermario18b

These are static html files that are to be presented as-is? (along with their associated images, scripts and css)

You’ve got a couple different options here:

  • You can use the ngx_http_auth_request_module to pass a subrequest to Django for authorization, allowing nginx to serve the files. See Module ngx_http_auth_request_module)

  • You can use the X-Sendfile feature to have the request get passed through to Django, which then returns a response with the X-Accel-Redirect header. See X-Accel | NGINX and XSendfile | NGINX.

  • You can add whitenoise to your Django app to actually serve the static files. You would then either need to modify the middleware to perform a security check before serving the file, or add a security check as a separate middleware class before the whitenoise middleware.

  • Last, and definitely least, you could allow Django itself to serve the static files. See Managing static files (e.g. images, JavaScript, CSS) | Django documentation | Django. Note, from the docs:

This is not suitable for production use! For some common deployment strategies, see Deploying static files.

Yes, exactly.
Thanks for your answer, I’ll check the options you gave me to understand what is best for my case.

supermario18b

I ended up using the first option (ngx_http_auth_request_module). It perfectly suits my needs.
I mark the topic as solved :slight_smile: