How to configure Django's "staticfiles" app to list directory contents?
Asked Answered
R

4

8

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?

Rompish answered 15/5, 2013 at 12:20 Comment(0)
I
8
'show_indexes': True

As per the documentation

Infestation answered 15/5, 2013 at 12:28 Comment(4)
@ArnarYngvason yes but it still works, github.com/django/django/blob/1.5/django/views/static.py#L23Infestation
Meaning, I should explicitly add an entry in urlpatterns (within urls.py) for it? I'll try that, but it feels redundant considering all the STATIC* configuration is done in settings.pyRompish
In fact django doesn't serve it because in normal usage, apache or nginx do it... Cleaner to enable a feature in development than to disable it in prod.Chaetopod
2.2 link docs.djangoproject.com/en/2.2/ref/views/…Anaclitic
R
12

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' 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.

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})
Rompish answered 15/5, 2013 at 13:50 Comment(1)
For debug mode it looks like static('foo', document_root='/bar', show_indexes=True).Raptor
I
8
'show_indexes': True

As per the documentation

Infestation answered 15/5, 2013 at 12:28 Comment(4)
@ArnarYngvason yes but it still works, github.com/django/django/blob/1.5/django/views/static.py#L23Infestation
Meaning, I should explicitly add an entry in urlpatterns (within urls.py) for it? I'll try that, but it feels redundant considering all the STATIC* configuration is done in settings.pyRompish
In fact django doesn't serve it because in normal usage, apache or nginx do it... Cleaner to enable a feature in development than to disable it in prod.Chaetopod
2.2 link docs.djangoproject.com/en/2.2/ref/views/…Anaclitic
C
1

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.

Chaetopod answered 15/5, 2013 at 12:27 Comment(0)
P
1

From https://docs.djangoproject.com/en/1.5/ref/views/#django.views.static.serve ...

static.serve(request, path, document_root, show_indexes=True)  
Possing answered 15/5, 2013 at 12:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.