almost a beginner almost a beginner - 3 months ago 17
Python Question

Django Invalid block tag: 'static' means what exactly?

(I'm using Django 1.3 - Python 1.7)

Been through all the related questions without any resolution.

The syntax error is in line 5, not line one:

{% load static %}
<html>
<head>
<link rel="stylesheet" href="{% static 'css/home.css' %}">


After reviewing related questions, some suggested first line should change to:

{% load staticfiles %}


I tried that, instead of "Invalid block tag: 'static'" error, I get :


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


I double checked the setting files:

STATIC_ROOT = '/home/username/mysite/static'
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

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',
'mysite.myapp',
)


Turned "Debug = False"

... and other suggestions. But I still get the same two errors.

The file structure is:

mysite contains the following folders/files: myapp
, media
, static
, settings.py
, ...

myapp contains the following folders/files: static
, templates
, views.py
, ...

both mysite -> static and myapp -> static contain identical folders and files.

Please ask if you require more information.

Any help/direction would be appreciated. Thanks.

Answer

In Django 1.3 there's no static tag, and you must use it like this:

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

or add static to TEMPLATE_CONTEXT_PROCESSORS and use STATIC_URL directly from template:

#  settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
    #  other processors
    ’django.core.context_processors.static’,
)

#  some.html
<img src="{{ STATIC_URL }}images/hi.jpg" />