django css和js文件

时间:2012-08-15 12:42:14

标签: django

我无法在我的django项目中使用CSS和JS文件。

文件夹结构:

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文件

运行python manage.py collectstatic返回:

"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?

1 个答案:

答案 0 :(得分:9)

您永远手动放置任何内容STATIC_ROOT。它只是生产中collectstatic 的倾销地;它甚至不应该存在于开发中。

您的所有静态资源都需要放在一个应用程序的static目录中,或者如果您需要项目范围的资源,则必须创建一个完全不同的目录,这与{{{} { 1}}或MEDIA_ROOT并将其添加到STATIC_ROOT

STATICFILES_DIRS

你可以随意调用它;我通常使用“资产”。只是不要将其命名为“静态”,“媒体”,“site_media”等,以避免混淆。