Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “如何显示字段”;标签";在django';s内置密码验证错误?_Python_Django_Django Templates_Django Authentication - Fatal编程技术网

Python “如何显示字段”;标签";在django';s内置密码验证错误?

Python “如何显示字段”;标签";在django';s内置密码验证错误?,python,django,django-templates,django-authentication,Python,Django,Django Templates,Django Authentication,我正在使用Django的内置身份验证类视图,需要自定义密码验证失败时显示的错误消息 例如,在内置中,如果我尝试将密码更改为测试,则我的模板中将显示以下错误: 新密码2 此密码太短。它必须至少包含8个字符 这个密码太常见了 我想将新密码2更改为新密码 以下是我的密码重置视图模板的相关部分: {% extends 'registration/base.html' %} {% block card_body %} <div class="form-group"

我正在使用Django的内置身份验证类视图,需要自定义密码验证失败时显示的错误消息

例如,在内置中,如果我尝试将密码更改为
测试
,则我的模板中将显示以下错误:

  • 新密码2

    • 此密码太短。它必须至少包含8个字符
    • 这个密码太常见了
我想将
新密码2
更改为
新密码

以下是我的密码重置视图模板的相关部分:

{% extends 'registration/base.html' %}

{% block card_body %}
  <div class="form-group">
    <label for="old_password">
      Old Password:
      {{ form.old_password }}
    </label>
  </div>
  <div class="form-group">
    <label for="new_password1">
      New Password:
      {{ form.new_password1 }}
    </label>
  </div>
  <div class="form-group">
    <label for="new_password2">
      Confirm Password:
      {{ form.new_password2 }}
    </label>
  </div>
  <div class="form-group">
    <input type="submit" value="Change" class="btn float-right login_btn">
  </div>
{% endblock card_body %}
{% block card_footer %}
  {% if form.errors %}
    <p class="d-flex justify-content-center links">
      {{ form.errors }}
    </p>
  {% endif %}
{% endblock card_footer %}
{%extends'注册/base.html%}
{%block card_body%}
旧密码:
{{form.old_password}}
新密码:
{{form.new_password1}}
确认密码:
{{form.new_password2}}
{%endblock卡片\正文%}
{%block card_footer%}
{%if form.errors%}

{{form.errors}}

{%endif%} {%endblock卡片\页脚%}
  • 为模板提供一些dict,将表单字段名称映射到所需的标签,如:

    字段\u映射={
    “旧密码”:“旧密码”,
    '新密码1':'新密码',
    “新密码2”:“确认密码”
    }
    
  • 只需手动迭代错误并使用映射dic将字段名称转换为所需的标签:

    {%用于表单中的字段\ u名称。错误:%}
    {{fields\u mapping[field\u name]}}:
    {%用于格式为的错误消息。错误[字段名称]:%}
    *{{err_message}}
    {%endfor%}
    {%endfor%}
    
  • 根据需要自定义HTML/CSS