almost a beginner almost a beginner - 1 year ago 138
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 %}
<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: ""
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

# Uncomment the next line to enable the admin:
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',

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
, ...

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

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 Source

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:

    #  other processors

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