10

I'm having a really strange issue trying to get the staticfiles taglib working in my application. I'm essentially getting the following error:

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

Here's my template which is throwing this error:

{% 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>

Here's my 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',
)

Essentially, I followed the guide available in Django's documentation on how to set up the static serving application, and got this error. Can anyone see what the issue is? Am I missing something?

Full stacktrace here.

Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
  • What happens when you go into the shell and type `import staticfiles`? – Jordan Reiter Sep 06 '11 at 19:43
  • I guess that you did not set the `STATIC_ROOT` value in settings. Does `manage.py collectstatic` work fine? – Ski Sep 06 '11 at 20:08
  • @Jordan Reiter, `ImportError: No module named staticfiles`. – Naftuli Kay Sep 06 '11 at 20:22
  • @Skirmantas, had that issue, fixed it, but still getting the error. – Naftuli Kay Sep 06 '11 at 20:22
  • Try `import django.contrib.staticfiles.templatetags.staticfiles` in `python manage.py shell` – Ski Sep 06 '11 at 20:31
  • There's no such package as "templatetags.staticfiles" in "django.contrib.staticfiles". I'm running things in Buildout, so is there another package other than Django proper which I need to include? I'm running Django 1.3. – Naftuli Kay Sep 06 '11 at 20:37
  • My guess is there's something amiss with your Django install. I'd do a reinstall of Django and see what happens. – Jordan Reiter Sep 06 '11 at 20:41
  • 1
    AFAIK This is new in the development version not in 1.3. See The first line for the [static tag in the docs](https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#static) and compare the doc to version 1.3. – mkriheli Sep 06 '11 at 21:09
  • I did find what seems to be the tag in: [django.templatetags.static.py](http://pastebin.com/RrRWihuC). – Naftuli Kay Sep 06 '11 at 21:10
  • That did the trick. It's weird that Django's docs default over to `dev` rather than their latest releas. – Naftuli Kay Sep 06 '11 at 21:18
  • Provide an answer detailing using `{% load static %}` and `{% get_static_prefix %}` and I'll give you the answer. – Naftuli Kay Sep 06 '11 at 21:19

1 Answers1

33

You probably are running django 1.3 right!?

So, notice the difference between django dev(1.4) and django 1.3:

So if you wanna use "{% load staticfiles %}" u must be using django dev(1.4+)

This is how to use in django 1.3 :

{% load static %}
<img src="{% get_static_prefix %}images/hi.jpg" />
Arthur Neves
  • 11,840
  • 8
  • 60
  • 73