Load static files for all templates in django

会有一股神秘感。 提交于 2019-12-04 18:09:00

问题


Is there a way in django to not need the {% load static %} at the top of every template?

This question indicates you can factor out common load tags into settings, but doesn't give the particulars you need in this case.


回答1:


As of django 1.9, you can add the following to your template settings:

'builtins': ['django.contrib.staticfiles.templatetags.staticfiles']

For example, the whole template setting might look like this:

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',
            ],
            'builtins': [
                'django.contrib.staticfiles.templatetags.staticfiles',
            ],
        },
    },
]


来源:https://stackoverflow.com/questions/35837134/load-static-files-for-all-templates-in-django

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