【发布时间】:2014-02-16 05:17:45
【问题描述】:
我在这上面花了几个小时,到处找,但无法弄清楚,所以我曾经向更有经验的人寻求帮助。
这不是我第一次创建 django 应用程序,所以我参考了我之前所做的,但没有任何帮助。我正在尝试使用“静态”模板标签提供静态文件,但它不起作用。
我很确定我正确设置了所有内容(按照教程和我以前的项目;我将在下面提供设置),但是当我在本地运行服务器时,它找不到我的静态文件。当我用 {{STATIC_URL}} 标签替换静态模板标签时,它可以工作。我什至在浏览器中输入静态文件的直接路径,它们都可以工作。我得到 CSS 和 JS 文件的输出。静态模板标签不起作用,这让我抓狂。
这是我的设置:
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = 'staticfiles'
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/w/static/'
# URL prefix for admin static files -- CSS, JavaScript and images.
# Make sure to use a trailing slash.
# Examples: "http://foo.com/static/admin/", "/static/admin/".
#ADMIN_MEDIA_PREFIX = '/admin/media/'
# Additional locations of static files
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, 'static/'),
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
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',)
顺便说一句,我检查了静态文件夹的路径,它是正确的。我故意不包括应用程序的名称,所以它就在那里。我还包括 {%load static from staticfiles%} 标签。正如我提到的,只有静态标签不起作用。请有人解释一下我犯了什么愚蠢的错误。
谢谢
【问题讨论】:
标签: django static django-templates