Make django static tag globally available

旧城冷巷雨未停 提交于 2019-12-12 00:29:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!