问题
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