TemplateSyntaxError 'staticfiles' is not a registered tag library 解决方案

分类:Python 新编程
pythondjango使用模板引擎的xxx.html文件中加入 {% load staticfiles %} 的时候报错信息如下:

ERROR log 230 Internal Server Error: /register/
Traceback (most recent call last):
  File "C:\Users\Administrator\Envs\blog\lib\site-packages\django\template\defaulttags.py", line 1034, in find_library
    return parser.libraries[name]
KeyError: 'staticfiles'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Administrator\Envs\blog\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Administrator\Envs\blog\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Administrator\Envs\blog\lib\site-packages\django\views\generic\base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\Administrator\Envs\blog\lib\site-packages\django\views\generic\base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\Administrator\test\blog\users\views.py", line 8, in get
    return render(request,'register.html')
  File "C:\Users\Administrator\Envs\blog\lib\site-packages\django\shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "C:\Users\Administrator\Envs\blog\lib\site-packages\django\template\loader.py", line 61, in render_to_string
    template = get_template(template_name, using=using)
  File "C:\Users\Administrator\Envs\blog\lib\site-packages\django\template\loader.py", line 15, in get_template
    return engine.get_template(template_name)
  File "C:\Users\Administrator\Envs\blog\lib\site-packages\django\template\backends\django.py", line 34, in get_template
    return Template(self.engine.get_template(template_name), self)
  File "C:\Users\Administrator\Envs\blog\lib\site-packages\django\template\engine.py", line 143, in get_template
    template, origin = self.find_template(template_name)
  File "C:\Users\Administrator\Envs\blog\lib\site-packages\django\template\engine.py", line 125, in find_template
    template = loader.get_template(name, skip=skip)
  File "C:\Users\Administrator\Envs\blog\lib\site-packages\django\template\loaders\base.py", line 30, in get_template
    contents, origin, origin.template_name, self.engine,
  File "C:\Users\Administrator\Envs\blog\lib\site-packages\django\template\base.py", line 155, in __init__
    self.nodelist = self.compile_nodelist()
  File "C:\Users\Administrator\Envs\blog\lib\site-packages\django\template\base.py", line 193, in compile_nodelist
    return parser.parse()
  File "C:\Users\Administrator\Envs\blog\lib\site-packages\django\template\base.py", line 478, in parse
    raise self.error(token, e)
  File "C:\Users\Administrator\Envs\blog\lib\site-packages\django\template\base.py", line 476, in parse
    compiled_result = compile_func(self, token)
  File "C:\Users\Administrator\Envs\blog\lib\site-packages\django\template\defaulttags.py", line 1091, in load
    lib = find_library(parser, name)
  File "C:\Users\Administrator\Envs\blog\lib\site-packages\django\template\defaulttags.py", line 1038, in find_library
    name, "\n".join(sorted(parser.libraries)),
django.template.exceptions.TemplateSyntaxError: 'staticfiles' is not a registered tag library. Must be one of:
admin_list
admin_modify
admin_urls
cache
i18n
l10n
log
static
tz
ERROR basehttp 157 "GET /register/ HTTP/1.1" 500 153520

我们仔细观察一下报错的信息如下:

admin_list
admin_modify
admin_urls
cache
i18n
l10n
log
static
tz

不难发现我们已经有了解决方案了,提示的信息让我们在这个列表中选一个名字,我发现有一个static 这个名字,那我们试一下,看看是否成功,修改如下:

{% load static %}

改完之后,程序就能正常工作了

分类:Python 新编程