问题
I'm trying to make the Django 'static' tag available in all templates. I've tried this suggestion:
from django.template.loader import add_to_builtins
add_to_builtins('django.contrib.staticfiles') # I've tried each of these
add_to_builtins('django.contrib.staticfiles.templatetags')
add_to_builtins('django.contrib.staticfiles.templatetags.staticfiles')
But keep getting the following error message:
django.template.base.InvalidTemplateLibrary: Template library django.contrib.staticfiles does not have a variable named 'register'
What am I doing wrong?
Thanks
回答1:
You need to add 'django.core.context_processors.static', in your settings.py like this:
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.core.context_processors.request',
'django.contrib.messages.context_processors.messages',
)
回答2:
Question is answered on SO here
来源:https://stackoverflow.com/questions/12758401/make-django-static-tag-globally-available