更改用户电子邮件地址
我想允许用户更改他的电子邮件地址。用户更改他的电子邮件地址,然后带有链接的确认电子邮件将发送到该地址,用户单击该链接后,它将更改他在数据库中的电子邮件地址。
我知道 django-generic-confirmation 处理这样的确认,但我想尝试自己做。
要更改电子邮件,我的代码将是:
User.objects.get(username=username).update(email=request.POST['email'])
要向该地址发送电子邮件,我将:
if 'Change Email' in request.POST.values():
from django.core.mail import send_mail
send_mail(
'Confirm email change',
'Click this **link** to confirm your change of email',
'[email protected]',
[request.POST['email']]
)
如何将数据库中电子邮件的更改延迟到用户确认其电子邮件之后?我如何创建一个链接来激活此过程的电子邮件?谢谢。
I would like to allow a user to change his email address. A user would change his email address, then a confirmation email with a link would be sent to that address, after the user clicked the link it would change his email address in the database.
I know there django-generic-confirmation deals with confirmations like this, but I would like to try and do it on my own.
To change an email, my code would be:
User.objects.get(username=username).update(email=request.POST['email'])
And to send an email to that address, I would have:
if 'Change Email' in request.POST.values():
from django.core.mail import send_mail
send_mail(
'Confirm email change',
'Click this **link** to confirm your change of email',
'[email protected]',
[request.POST['email']]
)
How would I delay the change of email in the db until only after the user has confirmed his email? And how would i create a link that activates the email for this process? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很简单。您想要做的是编写一个应用程序,其中至少有一个如下所示的模型;
使用 UUID4 填写 auth_key,您可以将其视为 urlconf 中的 slug 字段。
然后编写一个视图,获取 auth_key,在数据库中搜索该密钥,并将用户的电子邮件更改为 new_email。完成后删除记录。
对于奖励积分,请添加过期时间,并偶尔清除任何超过一天的记录。
您可能还想考虑使用安全问题类型系统。许多人想要更改电子邮件的主要原因是旧电子邮件已不复存在。例如离开公司、邮件主机消失(现在这种情况不太常见)等等。
This is pretty simple. What you want to do is write an app with at least a model that looks like this;
Fill in the auth_key with a UUID4 and you can treat it like you would a slug field in your urlconf.
Then write a view that takes the auth_key, searches for that in the database, and changes the user's email to new_email. Delete the record when you're done.
For bonus points, add in an expiration on it, and occasionally clean out any records older than a day.
You might also want to think about a security question type system instead. As the main reason a lot of people might want to change their email is that the old one no longer exists. For example leaving a company, a mail host disappearing (thats way less common now) etc, etc, etc.
这看起来很愚蠢。您等到他们确认电子邮件为止。也许这不是你真正要问的。
这没什么意义。想必您是在询问如何填写
**link**
。如果是这样,这就是 Django
reverse()
函数的用途。为此编写一个视图函数。
为视图函数选择一个合理的 URL 路径。
使用
reverse
生成插入到电子邮件中的完整 URL。That seems silly. You wait until they confirm the email. Maybe that's not what you're really asking.
That makes little sense. Presumably, you're asking how to fill in
**link**
.If so, that's what the Django
reverse()
function is for.Write a view function for this.
Pick a sensible URL path for the view function.
Use
reverse
to generate the complete URL that gets inserted into the email.