2

I am unable to use CSS and JS files in my django project.

Folder structure:

mysite/
mysite/mysite/settings.py
mysite/mysite/templates/base.html
mysite/mysite/assets/css/...

settings.py

PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL = 'static'
STATICFILES_DIRS = ()
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)

urls.py

from django.conf.urls import patterns, include, url
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',...)

if settings.DEBUG:
  urlpatterns += staticfiles_urlpatterns()

base.html

running python manage.py collectstatic returns:

"Your STATICFILES_DIRS setting is not a tuple or list; "
django.core.exceptions.ImproperlyConfigured: Your STATICFILES_DIRS setting is not a tuple or list; perhaps you forgot a trailing comma?
2
  • Where is your static folder? Have you run collectstatic? STATIC_URL should be /static/ Commented Aug 15, 2012 at 12:44
  • also, STATIC_ROOT should have a trailing /: STATIC_ROOT = os.path.join(PROJECT_PATH, 'static/') Commented Aug 15, 2012 at 12:59

1 Answer 1

9

You never manually put anything STATIC_ROOT. It is only a dumping ground for collectstatic in production; it shouldn't even exist in development.

All of your static resources need to go in one of your apps' static directories, or if you need project-wide resources, you must create an entirely different directory for that is not the same as either MEDIA_ROOT or STATIC_ROOT and add it to STATICFILES_DIRS:

STATICFILES_DIRS = (
    os.path.join(PROJECT_PATH, 'assets'),
)

You can call it whatever you want; I typically use "assets". Just don't name it "static", "media", "site_media", etc., just to avoid confusion.

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

6 Comments

I moved the files to mysite/mysite/assets, changed static_files_dirs as you described, but I get ""Your STATICFILES_DIRS setting is not a tuple or list; " django.core.exceptions.ImproperlyConfigured: Your STATICFILES_DIRS setting is not a tuple or list; perhaps you forgot a trailing comma?"
If you copied and pasted what I had, you would not be getting that error. What did you change?
I moved the assets folder so that it matches the path of PROJECT_PATH , that's it.
hhmmm adding the "," after os.path.join() did the trick it seams. Weird
@kristiannissen yes, because (1,) is a tuple (as required for that particular setting) whereas (1) is interpreted as 1.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.