Hey everyone! Sorry if this seems repetitive but I really did try to get this to work the best I could using the solutions above. Nothing seems to work and I'm hitting a wall.
I am trying to simply customize the email sent when the password-reset
route is hit. I want it to direct to a route in my frontend (React).
I tried @mohmyo solution to customize the email here https://github.com/iMerica/dj-rest-auth/issues/9#issuecomment-598963520.
This doesn't seem to do a thing sadly.
Here's some code from my project:
project/users/serializers.py - This is my custom serializer:
from dj_rest_auth.serializers import PasswordResetSerializer
class CustomPasswordResetSerializer(PasswordResetSerializer):
def get_email_options(self):
return {
'email_template_name': 'registration/password_reset_message.html'
}
project/users/urls.py - users is a app
from dj_rest_auth.registration.views import (ConfirmEmailView, RegisterView,
VerifyEmailView)
from dj_rest_auth.views import LoginView, LogoutView
from django.urls import path, re_path
urlpatterns = [
path('register/', RegisterView.as_view()),
path('account-confirm-email/<str:key>/', ConfirmEmailView.as_view()),
path('login/', LoginView.as_view()),
path('logout/', LogoutView.as_view()),
path('verify-email/',
VerifyEmailView.as_view(), name='rest_verify_email'),
path('account-confirm-email/',
VerifyEmailView.as_view(), name='account_email_verification_sent'),
re_path(r'^account-confirm-email/(?P<key>[-:\w]+)/$',
VerifyEmailView.as_view(), name='account_confirm_email'),
]
project/mysite/urls.py - mysite is the directory where settings.py
and the the main urls.py
are:
from dj_rest_auth.views import PasswordResetConfirmView, PasswordResetView
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('api/users/', include('users.urls')),
path('api/users/password-reset/', PasswordResetView.as_view()),
path('api/users/password-reset-confirm/<uidb64>/<token>/',
PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
]
project/mysite/settings.py
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR, "templates")],
...
}
]
# Django All Auth config
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
AUTHENTICATION_BACKENDS = (
"django.contrib.auth.backends.ModelBackend",
"allauth.account.auth_backends.AuthenticationBackend",
)
SITE_ID = 1
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_SESSION_REMEMBER = True
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_CONFIRM_EMAIL_ON_GET = True
LOGIN_URL = 'http://localhost:8000/api/v1/users/login/'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = local_settings.email_host
EMAIL_HOST_USER = local_settings.email_host_user
EMAIL_HOST_PASSWORD = local_settings.email_host_password
EMAIL_PORT = local_settings.email_port
REST_AUTH_SERIALIZERS = {
'PASSWORD_RESET_SERIALIZER': 'users.serializers.CustomPasswordResetSerializer'
}
I have a template in project/templates/registration/password_reset_message.py
.
This is my file structure if it helps.
├── manage.py
├── templates
│ └── registration
│ └── password_reset_message.html
├── mysite
│ ├── init.py
│ ├── asgi.py
│ ├── local_settings.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── users
├── init.py
├── admin.py
├── apps.py
├── forms.py
├── migrations
│ ├── 0001_initial.py
│ └── init.py
├── models.py
├── serializers.py
├── tests.py
├── urls.py
└── views.py
The issue is the template I specify isn't being sent. Rather, the default one provide by Django is.
I tried the other way in @mohmyo solution as well where you overwrite the entire save method of the serializer with no luck. Thanks to anyone who can help out here. This is my first GitHub issue!