我无法在我的 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()
基础.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_ROOT
或 STATIC_ROOT
并将其添加到 STATICFILES_DIRS
:
STATICFILES_DIRS = (
os.path.join(PROJECT_PATH, 'assets'),
)
你可以随便叫它;我通常使用“ Assets ”。只是不要将其命名为“static”、“media”、“site_media”等,以免混淆。
关于django css 和 js 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11969458/