How to configure Django's "staticfiles" app to list directory contents?
Just for completeness since it might help others, this is what I did to solve the problem.
Following @Hedde's answer, I went to use show_indexes
:
settings.py
- Kept all the configuration the same (i.e. all the
STATIC*
variables) - Removed
'django.contrib.staticfiles'
fromINSTALLED_APPS
The problem is that I cannot specify the show_indexes
parameter using Django's "built-in" method for static file configuration (via settings.py
). By having 'django.contrib.staticfiles'
in INSTALLED_APPS
, Django would create the static file handler with show_indexes = False
, ignoring my urlpatterns
.
urls.py
Added the following to urlpatterns:
url(regex = r'^%s(?P<path>.*)$' % settings.STATIC_URL[1:],
view = 'django.views.static.serve',
kwargs = {'document_root': '/abs/path/to/static/dir',
'show_indexes' : True})
'show_indexes': True
As per the documentation
Those files are not meant to be served by django. Show indexes is a configuration parameter of apache/nginx.
In production, with nginx, just add to the static serving part :
location ^~ /static/ {
autoindex on;
root /var/www/static_dir;
if ($query_string) {
expires max;
}
}
For dev environnement, Hedde's answer is indeed the good answer, but the display may not be the exact same than the one offered by your HTTP server. Don't rely on it's look&feel.