Django 管理员密码更改
Posted
技术标签:
【中文标题】Django 管理员密码更改【英文标题】:Django Admin Password Change 【发布时间】:2021-10-18 19:37:25 【问题描述】:第 1 步:我登录了 Django Admin 第 2 步:在管理员中单击密码更改 第 3 步:输入旧密码和新密码 第 4 步:单击“更改我的密码”按钮
已在数据库中成功更改密码。但是,我收到以下错误:
Environment:
Request Method: GET
Request URL: http://localhost:8000/admin/password_change/done/
Django Version: 2.2.24
Python Version: 3.7.10
Installed Applications:
['material.admin',
'material.admin.default',
'nested_inline',
'django.contrib.auth',
'django.contrib.sites',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_extensions',
'softdelete',
'reversion',
'actstream',
'rest_framework',
'rest_framework.authtoken',
'valital.api.apps.ApiConfig',
'cacheops',
'corsheaders',
'drf_yasg',
'storages',
'djmoney',
'mail_factory',
'mjml']
Installed Middleware:
['corsheaders.middleware.CorsMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'reversion.middleware.RevisionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Template error:
In template /usr/local/lib/python3.7/site-packages/material/admin/templates/registration/password_change_done.html, error at line 3
Invalid block tag on line 3: 'translate', expected 'elif', 'else' or 'endif'. Did you forget to register or load this tag?
1 : % extends "admin/base_site.html" %
2 : % load i18n %
3 : % block userlinks %% url 'django-admindocs-docroot' as docsroot %% if docsroot %<a href=" docsroot "> % translate 'Documentation' % </a> / % endif %% translate 'Change password' % / <a href="% url 'admin:logout' %">% translate 'Log out' %</a>% endblock %
4 : % block breadcrumbs %
5 : <div class="breadcrumbs">
6 : <a href="% url 'admin:index' %">% translate 'Home' %</a>
7 : › % translate 'Password change' %
8 : </div>
9 : % endblock %
10 :
11 : % block content %
12 : <div class="app">
13 : <div class="card">
Traceback:
File "/usr/local/lib/python3.7/site-packages/django/template/base.py" in parse
470. compile_func = self.tags[command]
During handling of the above exception ('translate'), another exception occurred:
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
145. response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
143. response = response.render()
File "/usr/local/lib/python3.7/site-packages/django/template/response.py" in render
106. self.content = self.rendered_content
File "/usr/local/lib/python3.7/site-packages/django/template/response.py" in rendered_content
81. template = self.resolve_template(self.template_name)
File "/usr/local/lib/python3.7/site-packages/django/template/response.py" in resolve_template
63. return select_template(template, using=self.using)
File "/usr/local/lib/python3.7/site-packages/django/template/loader.py" in select_template
42. return engine.get_template(template_name)
File "/usr/local/lib/python3.7/site-packages/django/template/backends/django.py" in get_template
34. return Template(self.engine.get_template(template_name), self)
File "/usr/local/lib/python3.7/site-packages/django/template/engine.py" in get_template
143. template, origin = self.find_template(template_name)
File "/usr/local/lib/python3.7/site-packages/django/template/engine.py" in find_template
125. template = loader.get_template(name, skip=skip)
File "/usr/local/lib/python3.7/site-packages/django/template/loaders/base.py" in get_template
30. contents, origin, origin.template_name, self.engine,
File "/usr/local/lib/python3.7/site-packages/django/template/base.py" in __init__
156. self.nodelist = self.compile_nodelist()
File "/usr/local/lib/python3.7/site-packages/django/template/base.py" in compile_nodelist
194. return parser.parse()
File "/usr/local/lib/python3.7/site-packages/django/template/base.py" in parse
478. raise self.error(token, e)
File "/usr/local/lib/python3.7/site-packages/django/template/base.py" in parse
476. compiled_result = compile_func(self, token)
File "/usr/local/lib/python3.7/site-packages/django/template/loader_tags.py" in do_extends
266. nodelist = parser.parse()
File "/usr/local/lib/python3.7/site-packages/django/template/base.py" in parse
478. raise self.error(token, e)
File "/usr/local/lib/python3.7/site-packages/django/template/base.py" in parse
476. compiled_result = compile_func(self, token)
File "/usr/local/lib/python3.7/site-packages/django/template/loader_tags.py" in do_block
209. nodelist = parser.parse(('endblock',))
File "/usr/local/lib/python3.7/site-packages/django/template/base.py" in parse
478. raise self.error(token, e)
File "/usr/local/lib/python3.7/site-packages/django/template/base.py" in parse
476. compiled_result = compile_func(self, token)
File "/usr/local/lib/python3.7/site-packages/django/template/defaulttags.py" in do_if
952. nodelist = parser.parse(('elif', 'else', 'endif'))
File "/usr/local/lib/python3.7/site-packages/django/template/base.py" in parse
472. self.invalid_block_tag(token, command, parse_until)
File "/usr/local/lib/python3.7/site-packages/django/template/base.py" in invalid_block_tag
528. get_text_list(["'%s'" % p for p in parse_until], 'or'),
Exception Type: TemplateSyntaxError at /admin/password_change/done/
Exception Value: Invalid block tag on line 3: 'translate', expected 'elif', 'else' or 'endif'. Did you forget to register or load this tag?
谁能告诉我一个答案来解决这个问题?
【问题讨论】:
【参考方案1】:你的错误信息说明了一切:
异常值:第 3 行的块标记无效:'translate'、预期的 'elif'、'else' 或 'endif'。
模板似乎不喜欢嵌套在if
中的translate
。
您可以 override the template 并仅覆盖 userlinks
块
% block userlinks %
% url 'django-admindocs-docroot' as docsroot %
% if docsroot %
<a href=" docsroot "> % translate 'Documentation' % </a>
/
% endif %
% translate 'Change password' %
/
<a href="% url 'admin:logout' %">% translate 'Log out' %</a>
% endblock %
注意https://docs.djangoproject.com/en/3.1/topics/i18n/translation/#translate-template-tag
更改为
Django 3.1
:
trans
标记已重命名为translate
。仍然支持trans
标记作为向后兼容的别名。
【讨论】:
显示到文件的路径指向 Docker 容器中的文件。由于我们不应该编辑 docker 容器中的文件,你能告诉我在我的本地机器中哪里可以找到这个文件。我已经尝试在我的本地机器上搜索这个文件,但我没有发现任何有用的东西。我在这个位置找到了一个文件“./.local/share/virtualenvs/app-backend-8zqIWoAn/lib/python3.7/site-packages/material/admin/templates/registration/password_change_done.html”。当我更改并保存此文件时,更改并未反映在登录页面中 docs.djangoproject.com/en/3.2/ref/contrib/admin/… @Sriram this question 可能有助于覆盖模板 发现错误:系统需要单词“tans”而不是“translate”(正如您所说的“看起来模板不喜欢嵌套在if
内的 translate
”)在文件中。感谢@Ralf 的回复
@Sriram 我编辑了答案以添加该信息;该标签已重命名为 v3.1
。显然,您在其中一个模板中发现别名似乎无法正常工作的错误以上是关于Django 管理员密码更改的主要内容,如果未能解决你的问题,请参考以下文章