3

I have this in the <head> of my base.html.

{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static "myStyleSheet.css" %}">

and I get error Invalid block tag: 'static'

Within INSTALLED_APPS I've included

'django.contrib.staticfiles',

and I've included within settings.py

STATIC_URL = '/static/' 
STATIC_ROOT = os.path.join(os.path.dirname(__file__), "static/")

Why do I get the load error?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
HenryM
  • 5,557
  • 7
  • 49
  • 105

6 Answers6

1

the actual problem here, I'm very sorry to say, was that within my app.yaml file I had specified a different directory for the static files and it seemed to be overriding everything else. Once removed, all sorted.

HenryM
  • 5,557
  • 7
  • 49
  • 105
0

Do you have the django.core.context_processors.static context processor in your TEMPLATE settings? Here's a sample:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'django.core.context_processors.static',
            ],
        },
    },
]

You can add it there or use { %load static %} in your template.

masnun
  • 11,635
  • 4
  • 39
  • 50
  • I've just tried adding it (to settings.py) but that hasn't made any difference – HenryM Jan 07 '16 at 18:53
  • You don't need to add the entire block, just find the `TEMPLATES` section, find the `context_processors` part and add that one to the last. – masnun Jan 07 '16 at 19:11
  • I've used the following : TEMPLATE_CONTEXT_PROCESSORS = ( 'django.contrib.auth.context_processors.auth', 'django.core.context_processors.request', 'django.core.context_processors.static', ) which is now giving me a different error, namely: Module "django.core.context_processors" does not define a "static" callable request processor – HenryM Jan 07 '16 at 19:27
  • What version of Django do you use? – masnun Jan 07 '16 at 19:37
0

Use {% load static %} or {% load static from staticfiles %}

Please check https://stackoverflow.com/a/27516199/263989

Community
  • 1
  • 1
fasouto
  • 4,386
  • 3
  • 30
  • 66
  • using either I get the message: 'static' is not a valid tag library: Template library static not found, tried django.templatetags.static,django.contrib.staticfiles.templatetags.static – HenryM Jan 07 '16 at 19:10
0

It seems that everything is fine. Check if static word is written using only English letters, it may be because of this.

pythad
  • 4,241
  • 2
  • 19
  • 41
0

You can change

href="{% static "myStyleSheet.css" %}"

to

href="{% static 'myStyleSheet.css' %}"

Single quotes for myStyleSheet.css

ruddra
  • 50,746
  • 7
  • 78
  • 101
M. Gar
  • 889
  • 4
  • 16
  • 33
0

Many answers are here but try this too. I'm going to describe everything. forget and clear anything added before and just follow these hints

1- in your settings.py and INSTALLED_APPS you should have
'django.contrib.staticfiles',

and also STATIC_URL = '/static/'. by default it's exists in the end of your file but check again yourself


2- in settings.py your TEMPLATES should be like as follow

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'django.core.context_processors.static',
            ],
        },
    },
]

Do not try to add this again ! just check for mistakes


3- static folder must be in the application directory not your main directory of project

project
    -application
        -migrations
        -static
        -templates

4- in static folder create sub directories for your files for example

project
    -application
        -static
            -css
            -js
            -img
            -....
            -....

and put your files to specific folder


5- now in your HTML file add {% load staticfiles %} first of file and everywhere you need a static file try to add that like as follow

<link href="{% static "css/myStyleSheet.css" %}" rel="stylesheet">

or

<link rel="icon" href="{% static "img/favicon.png" %}">

If you follow these hints, everything should be OK. test and report again

Arash Hatami
  • 5,297
  • 5
  • 39
  • 59