覆盖管理模板时出现 TemplateSyntaxError

Posted

技术标签:

【中文标题】覆盖管理模板时出现 TemplateSyntaxError【英文标题】:TemplateSyntaxError when overriding admin templates 【发布时间】:2020-04-21 14:36:53 【问题描述】:

我需要从 Django Admin 覆盖 index.html 模板。按照文档,我在 templates/admin 目录中创建了一个 index.html 文件,并从此处粘贴了原始内容 https://github.com/django/django/blob/master/django/contrib/admin/templates/admin/index.html

我收到了这个错误:

TemplateSyntaxError at /admin/

Invalid block tag on line 31: 'translate', expected 'elif', 'else' or 'endif'. Did you forget to register or load this tag?

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/admin/

Django Version: 3.0.1
Python Version: 3.6.8
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'app']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template /home/croves/code/cita-django/cita/templates/admin/index.html, error at line 31
   Invalid block tag on line 31: 'translate', expected 'elif', 'else' or 'endif'. Did you forget to register or load this tag?
   21 :         </caption>
   22 :         % for model in app.models %
   23 :             <tr class="model- model.object_name|lower ">
   24 :             % if model.admin_url %
   25 :                 <th scope="row"><a href=" model.admin_url "> model.name </a></th>
   26 :             % else %
   27 :                 <th scope="row"> model.name </th>
   28 :             % endif %
   29 : 
   30 :             % if model.add_url %
   31 :                 <td><a href=" model.add_url " class="addlink"> % translate 'Add' % </a></td>
   32 :             % else %
   33 :                 <td>&nbsp;</td>
   34 :             % endif %
   35 : 
   36 :             % if model.admin_url %
   37 :                 % if model.view_only %
   38 :                 <td><a href=" model.admin_url " class="viewlink">% translate 'View' %</a></td>
   39 :                 % else %
   40 :                 <td><a href=" model.admin_url " class="changelink">% translate 'Change' %</a></td>
   41 :                 % endif %


Traceback (most recent call last):
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/base.py", line 469, in parse
    compile_func = self.tags[command]

During handling of the above exception ('translate'), another exception occurred:
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/core/handlers/base.py", line 145, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/core/handlers/base.py", line 143, in _get_response
    response = response.render()
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/response.py", line 105, in render
    self.content = self.rendered_content
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/response.py", line 81, in rendered_content
    template = self.resolve_template(self.template_name)
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/response.py", line 65, in resolve_template
    return get_template(template, using=self.using)
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/loader.py", line 15, in get_template
    return engine.get_template(template_name)
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/backends/django.py", line 34, in get_template
    return Template(self.engine.get_template(template_name), self)
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/engine.py", line 143, in get_template
    template, origin = self.find_template(template_name)
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/engine.py", line 125, in find_template
    template = loader.get_template(name, skip=skip)
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/loaders/base.py", line 30, in get_template
    contents, origin, origin.template_name, self.engine,
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/base.py", line 156, in __init__
    self.nodelist = self.compile_nodelist()
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/base.py", line 194, in compile_nodelist
    return parser.parse()
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/base.py", line 477, in parse
    raise self.error(token, e)
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/base.py", line 475, in parse
    compiled_result = compile_func(self, token)
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/loader_tags.py", line 266, in do_extends
    nodelist = parser.parse()
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/base.py", line 477, in parse
    raise self.error(token, e)
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/base.py", line 475, in parse
    compiled_result = compile_func(self, token)
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/loader_tags.py", line 209, in do_block
    nodelist = parser.parse(('endblock',))
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/base.py", line 477, in parse
    raise self.error(token, e)
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/base.py", line 475, in parse
    compiled_result = compile_func(self, token)
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/defaulttags.py", line 952, in do_if
    nodelist = parser.parse(('elif', 'else', 'endif'))
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/base.py", line 477, in parse
    raise self.error(token, e)
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/base.py", line 475, in parse
    compiled_result = compile_func(self, token)
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/defaulttags.py", line 811, in do_for
    nodelist_loop = parser.parse(('empty', 'endfor',))
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/base.py", line 477, in parse
    raise self.error(token, e)
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/base.py", line 475, in parse
    compiled_result = compile_func(self, token)
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/defaulttags.py", line 811, in do_for
    nodelist_loop = parser.parse(('empty', 'endfor',))
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/base.py", line 477, in parse
    raise self.error(token, e)
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/base.py", line 475, in parse
    compiled_result = compile_func(self, token)
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/defaulttags.py", line 952, in do_if
    nodelist = parser.parse(('elif', 'else', 'endif'))
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/base.py", line 471, in parse
    self.invalid_block_tag(token, command, parse_until)
  File "/home/croves/code/cita-django/virtualenv/lib/python3.6/site-packages/django/template/base.py", line 527, in invalid_block_tag
    get_text_list(["'%s'" % p for p in parse_until], 'or'),

Exception Type: TemplateSyntaxError at /admin/
Exception Value: Invalid block tag on line 31: 'translate', expected 'elif', 'else' or 'endif'. Did you forget to register or load this tag?

【问题讨论】:

但我从原件复制/粘贴。为什么默认模板不会发生此错误? 您是否逐字粘贴了整个文件?您是否在模板顶部加载 i18n 您似乎也没有安装LocaleMiddleware。见docs.djangoproject.com/en/3.0/topics/i18n/translation 和docs.djangoproject.com/en/3.0/ref/middleware/… @Selcuk 是的,我确实复制/粘贴了整个文件并加载了 i18n。我添加了中间件,但没有解决问题 【参考方案1】:

我使用的是 Django 3.0.1,但在 GitHub 中复制了 3.0.2 的模板

【讨论】:

以上是关于覆盖管理模板时出现 TemplateSyntaxError的主要内容,如果未能解决你的问题,请参考以下文章

添加水印时出现FFmpeg错误:未覆盖 - 退出

在 Typescript 项目上运行业力覆盖时出现意外令牌

Flutter 使用 Provider 时出现“EditableText 上方不存在覆盖小部件”错误

如何覆盖右键单击 WebView2 控件时出现的 ContextMenu?

打开经常模板时出现运行时错误

Kotlin:尝试在 WebView 上添加 onReceivedError 时出现“修改器‘覆盖’不适用于‘本地函数’”错误