TemplateSyntaxError at /statistics/ ‘staticfiles‘ is not a registered tag li

Error django.template.exceptions.TemplateSyntaxError:'staticfiles' is not a registered tag library. Mustbe one of:
admin_list,
admin_modify,
etc.
This problem may be the use of the previous 2 points (such as 2.1) version of django, replaced with 3 . Several versions (such as 3.0.7) caused.
Solution:
Specify
OPTIONS in TEMPLATES in staticfiles settings.py file and add the following code

 'libraries': {
    
    
                'staticfiles': 'django.templatetags.static',
            },

The complete file is as follows:

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',
            ],
            'libraries': {
    
    
                'staticfiles': 'django.templatetags.static',
            },
        },
    },
]

The above is the solution steps. The following is a preliminary reading of TEMPLATES.
According to official documents and blogs, I will explain the templates part of this setting module:

TEMPLATES = [
    {
    
    
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
    
    
            # ... some options here ...
        },
    },
]

BACKEND is a point Python path of the template engine class that implements the Django template backend interface . The built-in backends are django.template.backends.django.DjangoTemplates and django.template.backends.jinja2.Jinja2.

Since most engines load templates from files, the top-level configuration of each engine contains two common settings:

DIRS defines a directory list, and the engine should search for template source files in the directory in the search order.
APP_DIRS tells the engine whether it should look for templates in installed applications. Each backend defines a general name for the subdirectory where its application should be stored in its template.
Although not common, multiple instances of the same backend can be configured with different options. In this case, you should NAME define a unique engine for each engine.

OPTIONS contains settings specific to the backend.
In the built-in backend there is
set BACKEND to configure the Django template engine with'django.template.backends.django.DjangoTemplates'.

If APP_DIRS is True, the DjangoTemplates engine will look for templates in the subdirectories of the installed application in templates. The common name is reserved for backward compatibility.
'libraries': The tag dictionary and dotted Python path of the template dictionary module to register with the template engine. This can be used to add new libraries or provide alternate labels for existing libraries. E.g:

OPTIONS={
    
    
    'libraries': {
    
    
        'myapp_tags': 'path.to.myapp.tags',
        'admin.urls': 'django.contrib.admin.templatetags.admin_urls',
    },
}

You can load the library by passing the corresponding dictionary key to the tag. {% load %}

The following is the content of other blogs . There is a more detailed introduction to loading static files. Here we quote content related to this issue.
In a web page, there is not only one html skeleton, but also css style files, js execution files, and some pictures. Therefore, loading static files in DTL is a problem that must be solved. In DTL, static tags are used to load static files. To use the static tag, you first need {% load static %}.
If you don’t want to use load to load the static tag every time you load a static file in the template, you can add'builtins' in TEMPLATES/OPTIONS in settings.py:['django. templatetags.static'], so that you can use static tags directly in the template in the future instead of manually loading.
Note: Do not add the wrong position

TEMPLATES = [
    {
    
    
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')]
        ,
        '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'
            ],
        },
    },
]

Guess you like

Origin blog.csdn.net/qq_45701131/article/details/107326861