【问题标题】:TemplateSyntaxError 'staticfiles' is not a valid tag library'TemplateSyntaxError 'staticfiles' 不是有效的标签库'
【发布时间】:2011-11-11 14:48:27
【问题描述】:

试图让staticfiles taglib 在我的应用程序中工作时遇到了一个非常奇怪的问题。我基本上收到以下错误:

'staticfiles' is not a valid tag library: Template library staticfiles not found, tried django.templatetags.staticfiles,django.contrib.admin.templatetags.staticfiles

这是我抛出此错误的模板:

{% load staticfiles %}
<html>
    <head>
        {% block stylesheets %}
        <link rel="stylesheet" href="{% static "styles/bootstrap-1.2.0.min.css" %}">
        {% endblock %}
        <title>{% block title %}Tzibor{% endblock %}</title>
    </head>
    <body>
        <h1>It Works!</h1>
        {% block scripts %}
        <script type="text/javascript" src="{% static "scripts/jquery-1.6.2.min.js" %}"></script>
        {% endblock %}
    </body>
</html>

这是我的settings.py

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (

)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', 
        'NAME': '/tmp/project.db',
        'USER': '',                    
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}

TIME_ZONE = 'UTC'
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

USE_I18N = True
USE_L10N = True
MEDIA_ROOT = '' # abs fs path to upload dir
MEDIA_URL = ''
STATIC_URL = '/static/'

ADMIN_MEDIA_PREFIX = '/media/'

SECRET_KEY = '4qo&twl!=ty!n%1@h2nidz^ie@$^uu@*pz)(ol%ise0&g6*@&_'

#TEMPLATE_CONTEXT_PROCESSORS = (
#   "django.contrib.auth.context_processors.auth",
#   "django.core.context_processors.debug",
#   "django.core.context_processors.i18n",
#   "django.core.context_processors.media",
#   "django.core.context_processors.static",
#   "django.contrib.messages.context_processors.messages",
#)

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
    'django.template.loaders.eggs.Loader',
)

TEMPLATE_DIRS = (

)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

ROOT_URLCONF = 'project.urls'

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',
    'django.contrib.admindocs',
    'django.contrib.staticfiles',
    'project.web',
    'south',
)

基本上,我遵循了可用的指南in Django's documentation on how to set up the static serving application, 并得到了这个错误。任何人都可以看到问题是什么?我错过了什么吗?

完整的堆栈跟踪here

【问题讨论】:

  • 当你进入 shell 并输入 import staticfiles 会发生什么?
  • 我猜你没有在设置中设置STATIC_ROOT 值。 manage.py collectstatic 工作正常吗?
  • @Jordan Reiter, ImportError: No module named staticfiles.
  • @Skirmantas,有这个问题,已修复,但仍然出现错误。
  • AFAIK 这是开发版本中的新功能,而不是 1.3。请参阅static tag in the docs 的第一行并将文档与版本 1.3 进行比较。

标签: django django-templates django-staticfiles


【解决方案1】:

您可能正在运行 django 1.3 对吧!?

所以,请注意 django dev(1.4) 和 django 1.3 之间的区别:

所以如果你想使用 "{% load staticfiles %}" 你必须使用 django dev(1.4+)

这是在 django 1.3 中的使用方法:

{% load static %}
<img src="{% get_static_prefix %}images/hi.jpg" />

【讨论】:

    猜你喜欢
    • 2019-09-19
    • 2011-05-31
    • 2012-08-22
    • 2012-04-02
    • 2012-03-21
    • 2013-08-21
    • 2020-08-08
    • 2020-04-03
    • 2021-09-09
    相关资源
    最近更新 更多