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

如何为Django-AllAuth的email.html模板添加样式?

自定义AllAuth email.html模板并添加样式

先在你的Django项目中创建templates/account/email.html文件(AllAuth会优先加载项目内的模板,覆盖官方默认模板),下面是一个包含完整功能、可自定义样式的模板示例:

{% extends "base.html" %} <!-- 替换成你项目的基础模板,保证风格统一 -->
{% load i18n %}
{% load account %}

{% block title %}{% trans "E-mail Addresses" %}{% endblock %}

{% block content %}
<style>
/* 这里添加自定义内联样式,也可以链接项目的外部CSS文件 */
.email-form-container {
    max-width: 600px;
    margin: 2rem auto;
    padding: 2rem;
    background-color: #f8f9fa;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.form-group {
    margin-bottom: 1.5rem;
}

.form-group label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 600;
    color: #333;
}

.form-group input[type="email"] {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid #ced4da;
    border-radius: 4px;
    font-size: 1rem;
}

.form-errors {
    color: #dc3545;
    margin-bottom: 1rem;
    padding: 0.75rem;
    background-color: #f8d7da;
    border-radius: 4px;
}

.submit-btn {
    background-color: #007bff;
    color: white;
    border: none;
    padding: 0.75rem 1.5rem;
    border-radius: 4px;
    font-size: 1rem;
    cursor: pointer;
}

.submit-btn:hover {
    background-color: #0056b3;
}

.email-list-item {
    margin: 1rem 0;
    padding: 1rem;
    background-color: white;
    border-radius: 4px;
    border-left: 4px solid #007bff;
}

.verified {
    color: #28a745;
    font-size: 0.9rem;
    margin-left: 0.5rem;
}

.unverified {
    color: #dc3545;
    font-size: 0.9rem;
    margin-left: 0.5rem;
}

.primary {
    color: #ffc107;
    font-size: 0.9rem;
    margin-left: 0.5rem;
}
</style>

<div class="email-form-container">
    <h1>{% trans "E-mail Addresses" %}</h1>

    <!-- 显示用户已绑定的邮箱列表 -->
    {% if user.emailaddress_set.all %}
    <p>{% trans '以下邮箱地址已与你的账号关联:' %}</p>

    <form action="{% url 'account_email' %}" class="email_list" method="post">
        {% csrf_token %}
        {% for emailaddress in user.emailaddress_set.all %}
        <div class="email-list-item">
            <label for="email_radio_{{forloop.counter}}">
                <input id="email_radio_{{forloop.counter}}" type="radio" name="email" 
                    {% if emailaddress.primary or user.emailaddress_set.count == 1 %}checked{%endif %} 
                    value="{{emailaddress.email}}"/>
                {{ emailaddress.email }}
                {% if emailaddress.verified %}
                <span class="verified">{% trans "已验证" %}</span>
                {% else %}
                <span class="unverified">{% trans "未验证" %}</span>
                {% endif %}
                {% if emailaddress.primary %}<span class="primary">{% trans "主邮箱" %}</span>{% endif %}
            </label>
        </div>
        {% endfor %}

        <div style="margin-top: 1.5rem;">
            <button style="margin-right: 1rem;" type="submit" name="action_primary" >{% trans '设为主邮箱' %}</button>
            <button style="margin-right: 1rem;" type="submit" name="action_send" >{% trans '重新发送验证邮件' %}</button>
            <button type="submit" name="action_remove" >{% trans '移除' %}</button>
        </div>
    </form>
    {% else %}
    <p><strong>{% trans '提示:'%}</strong> {% trans "你当前未绑定任何邮箱。建议添加邮箱以接收通知、重置密码等。" %}</p>
    {% endif %}

    <!-- 添加新邮箱的表单 -->
    <h2 style="margin-top: 2rem;">{% trans "添加新邮箱地址" %}</h2>

    <form method="post" action="{% url 'account_email' %}" class="add_email">
        {% csrf_token %}
        <!-- 显示表单全局错误 -->
        {% if form.non_field_errors %}
        <div class="form-errors">
            {{ form.non_field_errors }}
        </div>
        {% endif %}
        <!-- 自定义渲染邮箱字段,替代{{ form.as_p }}以控制样式 -->
        <div class="form-group">
            {{ form.email.label_tag }}
            {{ form.email }}
            {% if form.email.errors %}
            <div class="form-errors">
                {{ form.email.errors }}
            </div>
            {% endif %}
        </div>
        <button class="submit-btn" name="action_add" type="submit">{% trans "添加邮箱" %}</button>
    </form>
</div>
{% endblock %}

关键说明

  • 模板继承:通过{% extends "base.html" %}复用项目基础模板,保证页面风格统一;如果不需要继承,也可以直接写独立模板。
  • 完整表单元素:
    • 必须保留{% csrf_token %},防止跨站请求伪造。
    • 渲染form.non_field_errors和字段错误form.email.errors,让用户能看到明确的错误提示。
    • 自定义渲染邮箱字段,替代{{ form.as_p }},可以给输入框、标签添加自定义CSS类,精准控制样式。
    • 保留了已绑定邮箱的列表操作(设为主邮箱、重新验证、移除),这是AllAuth邮箱管理的核心功能。
  • 样式控制:
    示例用内联<style>添加样式,你也可以把样式写在项目的CSS文件中,通过基础模板引入。
    给容器、表单组、按钮等元素添加类名,方便后续调整样式。
  • 官方模板取舍:
    必须保留:csrf_token、表单提交路径(action="{% url 'account_email' %}")、邮箱列表渲染逻辑、错误信息展示。
    可自定义:文案(用{% trans %}替换成所需语言)、CSS类名、布局结构(根据你的页面需求调整)。

后续操作

  1. 确保项目settings.py中TEMPLATES的DIRS包含模板所在目录,让Django能找到这个自定义模板。
  2. 测试邮箱修改、验证流程,检查样式和功能是否正常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 21:44:50