django css and js files

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?

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.

链接地址: http://www.djcxy.com/p/59778.html

上一篇: 将python django项目1.3升级到1.5

下一篇: Django的CSS和JS文件