Marshall Davis
Marshall Davis

Reputation: 3650

Django runserver not serving some static files

My local testing server for Django v1.8.2 on Windows 8.1 is only serving certain static files. Others are giving a 404 error.

#urls.py - excerpt
urlpatterns = [
    url(r'^$', views.index)
] + staticfiles_urlpatterns()

#settings.py - excerpt
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'main',
    'users'
)

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

As far as I can tell the development server is configured to serve static files. When running runserver I get no warnings, and my code displays no syntax errors (import statements do exist and so forth). I did for testing purposes run collectstatic before starting the server just to be sure at least once.

My view template is:

{%  load staticfiles %}
<img src="{%  static 'main/img/ComingSoon.jpg' %}" alt="Coming Soon">

The link generated is /static/main/img/ComingSoon.jpg which looks correct, the file does exist in that location. What perplexes me is that this directory produces a 404 error, but other static files are served. The directory hierarchy is:

static/
  admin/
    css/
      ..
    js/
      ..
    img/
      ..
  main/
    img/
      ComingSoon.jpg

The URL localhost:8000/static/admin/img/ gives an expected message about indexes being not allowed. However, localhost:8000/static/main/img/ reports \main\img\ cannot be found. Both are 404 statuses. Images within static/admin/img/ do display correctly with the same links as what is giving an error. The Administration site for Django does display correctly.

Why would one directory within static/ not be indexed or accessible?

Upvotes: 3

Views: 993

Answers (1)

Marshall Davis
Marshall Davis

Reputation: 3650

According to the Django documentation regarding static/:

This should be an initially empty destination directory for collecting your static files from their permanent locations into one directory for ease of deployment; it is not a place to store your static files permanently. You should do that in directories that will be found by staticfiles’s finders, which by default, are 'static/' app sub-directories and any directories you include in STATICFILES_DIRS).

I moved the files to another directory such as:

myApp/
  main/
    static/
      main/
        img/
          ..
  static/
    ..

After running collectstatic I noticed the script created the subdirectories in myApp/static/ as expected and it must have generated the URLs needed because it now properly serves the files.

Upvotes: 2

Related Questions