Load static files for all templates in django

后端 未结 2 692
独厮守ぢ
独厮守ぢ 2021-02-07 11:56

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 se

2条回答
  •  迷失自我
    2021-02-07 12:13

    The previous answer's method is deprecated as of Django 3.0. (see : https://docs.djangoproject.com/en/3.0/releases/3.0/#features-removed-in-3-0)

    Now you'd need to add the following to your template settings:

    'builtins': ['django.templatetags.static']
    

    This is the updated templates setting:

    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.templatetags.static',
                ],
            },
        },
    ]
    

提交回复
热议问题