Search code examples
python-3.xdjango-rest-frameworkdjango-templatesdjoser

Djoser override activation email with template


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!" %}

Solution

  • 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