2

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

文件夹结构:

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

设置.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',
)

网址.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?
4

1 回答 1

9

你永远不会手动放任何东西STATIC_ROOT。它只是collectstatic 生产中的倾倒场;它甚至不应该存在于开发中。

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

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

你可以随便叫它;我通常使用“资产”。只是不要将其命名为“static”、“media”、“site_media”等,以免混淆。

于 2012-08-15T14:35:08.623 回答