I'm using Django's built-in web server, in DEBUG mode.
This is part of my settings.py
:
STATIC_ROOT = '/home/user/static_root'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
'/abs/path/to/static/dir',
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
If I access http://localhost:8000/static/png/
, I would expect to see a list of files available in /abs/path/to/static/dir/png
. Instead I get a 404 error "Directory indexes are not allowed here."
Now if I access the files directly, e.g. http://localhost:8000/static/png/test.png
, it works.
I've already checked some answers (here) without success.
So, does anyone know how to configure Django such that the staticfiles
app lists directory contents?
Unfortunately, the Django development server doesn't serve media files by default. Fortunately, there's a very simple workaround: You can add the media root as a static path to the ROOT_URLCONF in your project-level URLs.
Configuring static filesMake sure that django.contrib.staticfiles is included in your INSTALLED_APPS . In your templates, use the static template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE . Store your static files in a folder called static in your app.
STATICFILES_DIRS is the list of folders where Django will search for additional static files aside from the static folder of each app installed.
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
:
STATIC*
variables)'django.contrib.staticfiles'
from INSTALLED_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
.
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})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With