Django 1.7 static files directory scan

Working on a personal project and trying to write a directory crawler with ajax / jquery for images to create a slideshow on a web page using Django 1.7.

I get the standard "Directory indexes are not allowed here". when trying to browse a directory containing images. Each image itself is served by a static file, but I can't seem to browse the directory that will go through them.

My settings.py

file has the following static properties.

STATIC_URL = '/resources/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "resources"),os.path.join(BASE_DIR, "<app_name>/resources"),)
STATICFILES_FINDERS = (
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
)

      

Images are located at Base_dir / resources / img / Frontpage / slideshow / img_name.jpg

I can load every image, but url localhost: 8000 / resources / img / frontpage / slideshow / returns "Directory indexes not allowed here." mistake.

I have gone through several other Stack Overflow answers and none of them solved the problem, so I turn to the more experienced django developers help.

+3


source to share


2 answers


In a (properly configured) production installation, static files are not served by Django, but by some front-end server (nginx, apache, whatever), so the directory listing is set up on the front-end server. When working with an embedded dev server, it is common to use a django application staticfiles

to serve static files. There is no directory listing in this app, but you can easily write one and add to yours urls.py

.



That being said, relying on checking the list of directories on the server is fragile at best - you depend on the permitted directory and on the specific server implementation. Why don't you just write a Django view that returns a json list of a directory? It takes less time than coding to "scan" a directory listing and is much more reliable.

+2


source


in settings.py



STATIC_URL = '/static/'
STATIC_ROOT = PROJECT_DIR +  '/static/'

      

0


source







All Articles