[django] Django - Static file not found

I've seen several posts for this issue but didn't found my solution.

I'm trying to serve static files within my Django 1.3 development environment.

Here are my settings

...
STATIC_ROOT = '/home/glide/Documents/django/cbox/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
  '/static/',
)
...

My urls.py

urlpatterns = patterns('',
...
  url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
    {'document_root', settings.STATIC_ROOT}
  ),
...
);

My /home/glide/Documents/django/cbox/static/ directory is like

css
  main.css
javascript
image

I get a 404 error when trying to access http://127.0.0.1:8000/static/css/main.css.

Do I have to specify patterns for css, javascript and images individually ?

This question is related to django static-files

The answer is


In your cmd type command python manage.py findstatic --verbosity 2 static It will give the directory in which Django is looking for static files.If you have created a virtual environment then there will be a static folder inside this virtual_environment_name folder. VIRTUAL_ENVIRONMENT_NAME\Lib\site-packages\django\contrib\admin\static. On running the above 'findstatic' command if Django shows you this path then just paste all your static files in this static directory. In your html file use JINJA syntax for href and check for other inline css. If still there is an image src or url after giving JINJA syntax then prepend it with '/static'. This worked for me.


Another error can be not having your app listed in the INSTALLED_APPS listing like:

INSTALLED_APPS = [
    # ...
    'your_app',
]

Without having it in, you can face problems like not detecting your static files, basically all the files involving your app. Even though it can be correct as suggested in the correct answer by using:

STATICFILES_DIRS = (adding/path/of/your/app)

Can be one of the errors and should be reviewed if getting this error.


Serving static files can be achieved in several ways; here are my notes to self:

  • add a static/my_app/ directory to my_app (see the note about namespacing below)
  • define a new top level directory and add that to STATICFILES_DIRS in settings.py (note that The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting)

I prefer the first way, and a setup that's close to the way defined in the documentation, so in order to serve the file admin-custom.css to override a couple of admin styles, I have a setup like so:

.
+-- my_app/
¦   +-- static/
¦   ¦   +-- my_app/
¦   ¦       +-- admin-custom.css
¦   +-- settings.py
¦   +-- urls.py
¦   +-- wsgi.py
+-- static/
+-- templates/
¦   +-- admin/
¦       +-- base.html
+-- manage.py
# settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

This is then used in the template like so:

# /templates/admin/base.html
{% extends "admin/base.html" %}
{% load static %}

{% block extrahead %}
    <link rel="stylesheet" href="{% static "my_app/admin-custom.css" %}">
{% endblock %}

During development, if you use django.contrib.staticfiles [ed: installed by default], this will be done automatically by runserver when DEBUG is set to True [...]

https://docs.djangoproject.com/en/1.10/howto/static-files/

When deploying, I run collectstatic and serve static files with nginx.


The docs which cleared up all the confusion for me:

STATIC_ROOT

The absolute path to the directory where collectstatic will collect static files for 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).

https://docs.djangoproject.com/en/1.10/ref/settings/#static-root


Static file namespacing

Now we might be able to get away with putting our static files directly in my_app/static/ (rather than creating another my_app subdirectory), but it would actually be a bad idea. Django will use the first static file it finds whose name matches, and if you had a static file with the same name in a different application, Django would be unable to distinguish between them. We need to be able to point Django at the right one, and the easiest way to ensure this is by namespacing them. That is, by putting those static files inside another directory named for the application itself.

https://docs.djangoproject.com/en/1.10/howto/static-files/


STATICFILES_DIRS

Your project will probably also have static assets that aren’t tied to a particular app. In addition to using a static/ directory inside your apps, you can define a list of directories (STATICFILES_DIRS) in your settings file where Django will also look for static files.

https://docs.djangoproject.com/en/1.10/howto/static-files/


I solve this problem by adding my project name in INSTALLED_APPS.enter image description here


There could be only two things in settings.py which causes problems for you.

1) STATIC_URL = '/static/'

2)

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

and your static files should lie under static directory which is in same directory as project's settings file.

Even then if your static files are not loading then reason is , you might have kept

DEBUG = False

change it to True (strictly for development only). In production just change STATICFILES_DIRS to whatever path where static files resides.


If you've have added the django-storages module (to support uploading files to S3 in your django app for instance), and if like me you did not read correctly the documentation of this module, just remove this line from your settings.py:

STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

Otherwise it will cause your static assets to be looked NOT on your local machine but remotely in the S3 bucket, including admin panel CSS, and thus effectively breaking admin panel CSS.


If your static URL is correct but still:

Not found: /static/css/main.css

Perhaps your WSGI problem.

? Config WSGI serves both development env and production env

==========================project/project/wsgi.py==========================

import os
from django.conf import settings
from django.contrib.staticfiles.handlers import StaticFilesHandler
from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
if settings.DEBUG:
    application = StaticFilesHandler(get_wsgi_application())
else:
    application = get_wsgi_application()

There could be following things in your settings.py:

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',

'DIRS': ["templates"],

'APP_DIRS': True,

'OPTIONS': {
# ... some options here ...
},
}, ]

OR

STATIC_URL = '/static/'

OR

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


{'document_root', settings.STATIC_ROOT} needs to be {'document_root': settings.STATIC_ROOT}

or you'll get an error like dictionary update sequence element #0 has length 6; 2 is required


I found that I moved my DEBUG setting in my local settings to be overwritten by a default False value. Essentially look to make sure the DEBUG setting is actually false if you are developing with DEBUG and runserver.


TEMPLATE_DIR=os.path.join(BASE_DIR,'templates')
STATIC_DIR=os.path.join(BASE_DIR,'static')

STATICFILES_DIRS=[STATIC_DIR]

STATICFILES_DIRS is used in development and STATIC_ROOT in production,

STATICFILES_DIRS and STATIC_ROOT should not have same folder name,

If you need to use the exact same static folder in development and production, try this method

include this in settings.py

import socket

HOSTNAME = socket.gethostname()

# if hostname same as production url name use STATIC_ROOT 
if HOSTNAME == 'www.example.com':
    STATIC_ROOT = os.path.join(BASE_DIR, "static/")

else:
    STATICFILES_DIRS = [
            os.path.join(BASE_DIR, 'static/'),
        ]

  1. You can remove the STATIC_ROOT line
  2. Or you can create another static folder in different directory. For suppose the directory is: project\static Now update:
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, 'project/static/')
    ]
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')

Whatever you do the main point is STATICFILES_DIRS and STATIC_ROOT should not contain same directory.

I know it's been a long time but hope the new buddies can get help from it