You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django自定义password_reset_confirm.html模板extends标签失效问题

修复Django密码重置确认模板加载问题

看起来你是想让密码重置页面复用admin的样式,但改了extends标签之后页面内容丢失了对吧?这通常是因为没有正确匹配admin模板的block结构,或者遗漏了必要的block声明。

下面是调整后的完整password_reset_confirm.html模板,直接替换你的内容就行:

{% extends "admin/base_site.html" %}
{% load i18n static %}

{% block extrastyle %}
    {{ block.super }}
    <link rel="stylesheet" type="text/css" href="{% static "admin/css/forms.css" %}">
{% endblock %}

{% block breadcrumbs %}
<div class="breadcrumbs">
    <a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
    &rsaquo; {% trans 'Password reset confirmation' %}
</div>
{% endblock %}

{% block content %}
<div id="content-main">
    {% if validlink %}
        <p>{% trans "Please enter your new password twice so we can verify you typed it correctly." %}</p>
        <form method="post">{% csrf_token %}
            <div class="form-row">
                {{ form.new_password1.errors }}
                <label for="id_new_password1">{% trans 'New password:' %}</label>
                {{ form.new_password1 }}
            </div>
            <div class="form-row">
                {{ form.new_password2.errors }}
                <label for="id_new_password2">{% trans 'Confirm password:' %}</label>
                {{ form.new_password2 }}
            </div>
            <div class="submit-row">
                <input type="submit" value="{% trans 'Change my password' %}">
            </div>
        </form>
    {% else %}
        <p>{% trans "The password reset link was invalid, possibly because it has already been used.  Please request a new password reset." %}</p>
    {% endif %}
</div>
{% endblock %}

为什么你的原模板会出问题?

我来给你拆解几个关键原因:

  • 遗漏了content block:admin的base_site.html核心内容都放在content block里,你原来的代码只写了breadcrumbs,没把重置密码的表单内容放到content block里,所以页面自然显示空白。
  • 没有继承admin的额外样式:加上extrastyle block并调用{{ block.super }},能确保admin的表单样式正常加载,不然表单会显得很混乱。
  • 模板语法不完整:你原代码里的{% ...是未闭合的不完整标签,这会直接导致模板渲染失败,必须把所有模板标签补全。

额外注意事项

  • 确认你的模板路径确实是templates/registration/password_reset_confirm.html,Django会优先加载你自定义的模板,而非默认的auth模板。
  • 如果还是有问题,可以打开Django的DEBUG模式查看日志,里面会明确提示模板渲染的错误(比如找不到block、标签语法错误等)。

内容的提问来源于stack exchange,提问作者Username

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:08:48