Django的CSS和JS文件

我无法在我的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?

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

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

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

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

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

上一篇: django css and js files

下一篇: django redistributable app with south