10

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!" %}
yeti blue
  • 261
  • 3
  • 16
  • Note: if you have inline CSS in your custom template, your HTML template may not load. So, you may want to look into this [answer](https://stackoverflow.com/a/73581733/15993687) – Art Sep 02 '22 at 11:20

6 Answers6

15

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

Pavlo Naumenko
  • 561
  • 5
  • 15
  • Thanks! this worked. Initially I tried it and couldn't get it to work since I put the activation file in a template folder that was in my project not the app. I ended up just taking a break from this issue and revisited it today. Works fine now :) – yeti blue Sep 04 '20 at 16:22
  • do you know how does one personalizes username/first_name in the overridden email? – Fed Jun 07 '21 at 16:18
  • 1
    @FedericoCapaldo If I got you right, you need to use {{user.first_name}} {{user.last_name}}. user is in context already – Pavlo Naumenko Jun 07 '21 at 19:53
  • djoser uses django-templated-mail wich uses django templates, so if you're getting template not found, check out the docs https://docs.djangoproject.com/en/3.2/topics/templates/#support-for-template-engines – Hack_Hut Aug 30 '21 at 16:24
3

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)

  • do you know how does one personalizes username/first_name in the overridden email? – Fed Jun 07 '21 at 16:16
2

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

Isaac T
  • 406
  • 4
  • 8
0

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

0

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

Vito
  • 1
  • 2
-3

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

jnovack
  • 7,629
  • 2
  • 26
  • 40
  • Hello new contributor! It would be helpful if you can describe what you did and why it worked. – jnovack Sep 27 '20 at 22:56
  • Djoser has an activation template, it uses it by default, so I found the template it's in djoser/templates/email, filename is activation.html I change the file content and save it. and it worked without any code change – Anas Ahmad Sep 28 '20 at 23:17
  • @Anas Ahmad That is not a good way to do it. If you reinstall Djoser `pip install djoser` that code you have written will get overwritten. – Abishek Oct 03 '20 at 17:28
  • it's not a problem, I save the html in myproject folders and I copy them after re-installation. – Anas Ahmad Oct 04 '20 at 18:13
  • Yes but it's overall bad practice, is what he is trying to say. Whenever you change something in a dependency, you should always make sure that's represented in your projects code, using overrides or similiar. – Thorvald Feb 17 '21 at 20:07