问题描述
在我的应用程序中,我正在使用Django Allauth。我没有任何用户注册表格。管理员将通过上传包含用户信息的Excel文件来注册用户。我已完成所有这些操作,并且通过自动生成密码将用户保存在用户表中。上传用户列表并将其保存在数据库中之后,我想向每个用户发送重置密码电子邮件。
In my application I am using Django Allauth. I don't have any registration form for users. The admin is going to register users by uploading an excel file that contains user info. I have done all of this and users are saved in the user table by auto generating passwords. After I upload user lists and save them in database, I want to send a reset password email to each user.
在allauth中重置密码,您首先需要进入重置页面帐户/密码/重置/
并输入您的电子邮件。然后会发送一封电子邮件,指导您更改密码帐户/密码/重置/密钥/(?P< uidb36> [0-9A-Za-z] +)-(?P< key> 。+)/
In allauth to reset password you first need to go to reset page account/password/reset/
and type your email. then an email is send which directs you to change your password account/password/reset/key/(?P<uidb36>[0-9A-Za-z]+)-(?P<key>.+)/
是否可以直接在应用程序内发送电子邮件?该网址包含一个我不知道如何生成的密钥!还是有更好的方法呢?
Is it possible to send the email directly within the app? The url contains a key that I don't know how to generate!! Or is there any better way to do that?
推荐答案
有可能。我的解决方案实现了一个用户模型post_save信号来调用Allauth密码重置视图,该视图将向用户发送电子邮件。首先要考虑的是使用户电子邮件地址在admin用户创建表单中成为必需(如)。然后使用以下代码:
It's possible. My solution implements a User model post_save signal to call the Allauth Password reset view which will send the user the email. The first thing to consider is to make the user email address mandatory in the admin user create form (as explained here). And then use this code:
from allauth.account.views import PasswordResetView
from django.conf import settings
from django.dispatch import receiver
from django.http import HttpRequest
from django.middleware.csrf import get_token
@receiver(models.signals.post_save, sender=settings.AUTH_USER_MODEL)
def send_reset_password_email(sender, instance, created, **kwargs):
if created:
# First create a post request to pass to the view
request = HttpRequest()
request.method = 'POST'
# add the absolute url to be be included in email
if settings.DEBUG:
request.META['HTTP_HOST'] = '127.0.0.1:8000'
else:
request.META['HTTP_HOST'] = 'www.mysite.com'
# pass the post form data
request.POST = {
'email': instance.email,
'csrfmiddlewaretoken': get_token(HttpRequest())
}
PasswordResetView.as_view()(request) # email will be sent!
这篇关于Django AllAuth-如何手动发送重置密码电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!