我正在开发一个使用 Django 作为后端、使用 next.js/react.js 作为前端的项目。 我遇到的问题是尝试使用前端的用户界面重置密码。 我发现的所有解决方案都涉及使用模板在后端服务器上创建 UI,但我不希望这样做。
我可以从 Django(dj_rest_auth) 收到以下电子邮件,其中提供了服务器域的链接。
Hello from MyApp!
You're receiving this email because you or someone else has requested a password reset for your user account.
It can be safely ignored if you did not request a password reset. Click the link below to reset your password.
http://localhost:8000/password-reset-confirm/k/cd01t1-dfsdfdfwer2/
我尝试像这样覆盖
PasswordResetSerializer
,但它不起作用。
from dj_rest_auth.serializers import PasswordResetSerializer
class MyPasswordResetSerializer(PasswordResetSerializer):
def get_email_options(self) :
return {
'email_template_name': 'http://localhost:3000/new-password'
}
url.py
path('password-reset/', PasswordResetView.as_view()),
path('password-reset-confirm/<uidb64>/<token>/',
PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
在我的 settings.py 文件中我有
...
REST_AUTH_SERIALIZERS = {
'PASSWORD_RESET_SERIALIZER': 'myapp.serializers.MyPasswordResetSerializer'
}
...
如何将重置密码电子邮件中的链接更改为将用户发送到客户端以创建新密码的链接?
必须通过Django模板来实现。
from django.contrib.auth.forms import PasswordResetForm
from dj_rest_auth.serializers import PasswordResetSerializer
from django.conf import settings
class MyPasswordResetSerializer(PasswordResetSerializer):
password_reset_form_class = PasswordResetForm
def get_email_options(self):
return {
"subject_template_name": "email/password_reset_subject.txt",
"email_template_name": "email/password_reset_message.html",
"html_email_template_name": "email/password_reset_message.html",
"extra_email_context": {
"settings": settings,
}
}
以上路径是通过Django Template连接起来的。
在我测试的环境中,我在authenticate应用程序中实现了它,因此它连接到authenticate/template/email/password_reset_message.html文件。
密码重置_主题.txt
password change
password_reset_message.html
<html>
<body>
<h1>hello</h1>
</body>
</html>