覆盖管理模板时出现TemplateSyntaxError

时间:2020-01-03 00:11:47

标签: django django-admin

我需要从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?

1 个答案:

答案 0 :(得分:0)

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