Djoser override activation email with template

Viewed 4248

I've been scouring for more information about how to do this, but there seems to be little to no documentation help.

Essentially want I want to do is make a new template for the activation email so the link can start with localhost:3000 instead of localhost:8000 (I'm using Vue for the frontend post request that's why)

I managed to find this: https://github.com/sunscrapers/djoser/blob/master/djoser/templates/email/activation.html but when I added it to my own project, the default Djoser template is still being used.

This is how my settings.py looks like:

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',
            ],
        },
    },
]

My activation URL which works if the 8000 is replaced by 3000 manually:

 'ACTIVATION_URL': 'registration/activate/{uid}/{token}',

templates/email/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!" %}
6 Answers

I suppose you need to override email.ActivationEmail, for example to core/email.py

from djoser import email


class ActivationEmail(email.ActivationEmail):
    template_name = 'email/activation.html'

and add it to your settings.py

DJOSER = {
    'EMAIL': {
            'activation': 'core.email.ActivationEmail'
    }
}

Here are emails which can be overwritten link

Without changing the Djoser email classes and settings.py, You can override the html templates in your project's template directory using the concepts here

You just simply have to create a new folder named email in your 'templates' folder and in it create html files with the exact names for the email templates that you would want to customize/override.

For example if you want to customize the password reset email, your custom file path should resemble this :

projectName/templates/email/password_reset.html

Here is a full list of all the Djoser email templates that you can customize

You can configure the DOMAIN and SITE_NAME in the settings.py file instead of changing the template. Just put DOMAIN = ('localhost:3000') and SITE_NAME = ('YOUR_SITE_NAME')

DOMAIN = ('localhost:3000') 
SITE_NAME = ('YOUR_SITE_NAME') 
DJOSER = {
    ...
}

Source: https://stackoverflow.com/a/62586798/6568539 (I found that we don't need to use "config" as mentioned in this answer)

I found this closed thread and it did do the job of allowing me to render out a custom template. Might want to check this

the answer from Pavlo Naumenko works for me too so thanks a lot. But just a small note: I have to change the name of the file (I couldn't use "activation.html" on my own template). If I use it, django keeps sending the default template. Don't know why. In case someone happens that too. I was using django 3.2.3 with djoser 2.1.0

it's working! I've just changed activation.html file in ~/.local/lib/python3.5/site-packages/djoser/templates/email without any other changes

Related