Djoser 使用模板覆盖激活电子邮件

yet*_*lue 4 django-templates python-3.x django-rest-framework djoser

我一直在寻找有关如何执行此操作的更多信息,但似乎几乎没有文档帮助。

本质上我想要做的是为激活电子邮件创建一个新模板,以便链接可以以 localhost:3000 而不是 localhost:8000 开头(我使用 Vue 作为前端发布请求,这就是为什么)

我设法找到了这个:https : //github.com/sunscrapers/djoser/blob/master/djoser/templates/email/activation.html 但是当我将它添加到我自己的项目时,仍然使用默认的 Djoser 模板。

这是我的 settings.py 的样子:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
Run Code Online (Sandbox Code Playgroud)

如果手动将 8000 替换为 3000,我的激活 URL 有效:

 'ACTIVATION_URL': 'registration/activate/{uid}/{token}',
Run Code Online (Sandbox Code Playgroud)

模板/电子邮件/activation.html:

{% block subject %}
{% blocktrans %}Account activation on {{ site_name }}{% endblocktrans %}
{% endblock subject %}

{% block text_body %}
{% blocktrans %}You're receiving this email becaus!!!!!!e you need to finish activation process on {{ site_name }}.{% endblocktrans %}

{% trans "Please go to the following page to activate account:" %}
{{ http }}://{{ localhost:3000 }}/{{ {% url 'registration/activate' uidb64=uid token=token %} }}

{% trans "Thanks for using our site!" %}
Run Code Online (Sandbox Code Playgroud)

Pav*_*nko 6

我想你需要覆盖email.ActivationEmail,例如core/email.py

from djoser import email


class ActivationEmail(email.ActivationEmail):
    template_name = 'email/activation.html'
Run Code Online (Sandbox Code Playgroud)

并将其添加到您的 settings.py

DJOSER = {
    'EMAIL': {
            'activation': 'core.email.ActivationEmail'
    }
}
Run Code Online (Sandbox Code Playgroud)

这是可以覆盖的电子邮件链接