更改用户电子邮件地址

发布于 2024-11-13 11:32:45 字数 768 浏览 2 评论 0原文

我想允许用户更改他的电子邮件地址。用户更改他的电子邮件地址,然后带有链接的确认电子邮件将发送到该地址,用户单击该链接后,它将更改他在数据库中的电子邮件地址。

我知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇纵 2024-11-20 11:32:45

这很简单。您想要做的是编写一个应用程序,其中至少有一个如下所示的模型;

class EmailChangeAuth(models.Model):
    auth_key = models.CharField(max_length=42)
    user = models.ForeignKey(User)
    new_email = models.CharField(max_length=256)

使用 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;

class EmailChangeAuth(models.Model):
    auth_key = models.CharField(max_length=42)
    user = models.ForeignKey(User)
    new_email = models.CharField(max_length=256)

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.

半岛未凉 2024-11-20 11:32:45

如何将数据库中电子邮件的更改延迟到用户确认其电子邮件之后?

这看起来很愚蠢。您等到他们确认电子邮件为止。也许这不是你真正要问的。

我如何创建一个链接来激活此过程的电子邮件?

这没什么意义。想必您是在询问如何填写**link**

如果是这样,这就是 Django reverse() 函数的用途。

  1. 为此编写一个视图函数。

  2. 为视图函数选择一个合理的 URL 路径。

  3. 使用 reverse 生成插入到电子邮件中的完整 URL。

How would I delay the change of email in the db until only after the user has confirmed his email?

That seems silly. You wait until they confirm the email. Maybe that's not what you're really asking.

And how would i create a link that activates the email for this process?

That makes little sense. Presumably, you're asking how to fill in **link**.

If so, that's what the Django reverse() function is for.

  1. Write a view function for this.

  2. Pick a sensible URL path for the view function.

  3. Use reverse to generate the complete URL that gets inserted into the email.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文