1

I want to be able to load my static files on my local server, but when I request them, the browser returns 404 for every resource.

From what I can understand, STATIC_URL is the url in which my static files will be served. And STATICFILES_FINDERS specifies how my static files will be discovered. I set STATICFILES_DIRS to search for the static directory at the project root, but it doesn't seem to be be finding it.

On my settings.py,

# Python 2.7.5, Django 1.6

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

STATIC_URL = '/static/'

STATICFILES_FINDERS = (
   'django.contrib.staticfiles.finders.FileSystemFinder',
   'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)

This is my directory structure:

.
|-- myapp
   |-- settings.py
   |-- ...
   static
   |-- images
   |-- javascript
   |-- stylesheets
0

1 Answer 1

1

Here are a couple of ideas:

  1. You need a server for your static files. Are you using Apache HTTP server? The easiest way to serve your static files is to alias them in the httpd.conf file:

    Alias /static/ /path/to/static/
    <Directory /path/to/static>
    Require all granted
    </Directory>
    
  2. You need to specify a STATIC_ROOT, which could be /path/to/your_project/static but then you probably want to put your current static files and folders somewhere else, because everything in STATIC_ROOT will be overwritten when you call manage.py collectstatic. I put all of my static files, such as Bootstrap, Tablesorter, images and icons in a folder called assets, then put assets in my STATICFILES_DIR list.

  3. Use manage.py collectstatic to collect all static files and put them in STATIC_ROOT so that Apache can find them. Static files for the admin site will be automatically copied even if you do not add them to the list of STATICFILES_DIR.

Check out this post I wrote, which has several links to Django documentation on the topic.

Sign up to request clarification or add additional context in comments.

2 Comments

The trailing slashes for the Alias directive should be balanced on the arguments when at a sub URL. That is use, 'Alias /static /path/to/static' and not 'Alias /static/ /path/to/static'. If they aren't, it can screw up how Apache maps URLs to the file system with the slash being dropped and the calculated path being wrong. This can result in either a Not Found or Forbidden HTTP error response depending on Apache configuration.
Thanks @GrahamDumpleton! I have edited the answer to balance the trailing slashes in the URL and the path. This particular example is from the Django how-to guide for using Apache with mod_wsgi and the trailing slashes are correctly balanced as you indicated. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.