尝试使用Django电子邮件确认来验证用户,但是某些用户在尝试确认其电子邮件时收到此错误 [英] Trying to verify a user with Django email confirmation, however some user's are getting this error when they try to confirm their email

查看:76
本文介绍了尝试使用Django电子邮件确认来验证用户,但是某些用户在尝试确认其电子邮件时收到此错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前问过这个问题的一个变种,但意识到我还不清楚.这是我试图更好地解释该问题的尝试.

I asked a variant of this question before, but realized I was very unclear. This is my attempt at doing a better job of explaining the issue.

我向用户的电子邮件发送了一个链接,以确认他们注册该网站后的注册.但是,一小部分用户在尝试确认其电子邮件时收到以下错误.

I send a link to a user's email to confirm sign-up after they have registered for the site. However, a small proportion of users are getting the following error when they try to confirm their email.

错误:

TypeError/activate/{uidb64}/{token}/
error
'AnonymousUser' object is not iterable

我认为问题在于激活功能无法识别它们.但是,如果我将其发送回登录页面,则不确定如何使此激活链接起作用.有什么想法吗?

I think the problem is that the activate function is not recognizing them. However, not sure how to make this activation link work if I send them back to the login page. Any ideas?

是否需要添加诸如如果不是user.is_authenticated:"之类的内容,然后发送重定向到登录名?但是,如何返回激活功能?在所有情况下,我的应用程序中的登录都是常规的.

Do I need to add something like 'if not user.is_authenticated:' then send redirect to login? But, how do I come back to activate function? Login is general in my app to all situations.

激活代码:

try:
    uid = force_text(urlsafe_base64_decode(uidb64))
    user = User.objects.get(pk=uid)
except(TypeError, ValueError, OverflowError, User.DoesNotExist):
    user = None
if user is not None and account_activation_token.check_token(user, token):
    .... do something
else:
    return HttpResponse('Activation link is invalid!')

在这里创建电子邮件:

def activation_email(request):
    if request.user.is_authenticated:
        user=request.user
        message = render_to_string('email.html', {
                'user':user,
                'token':account_activation_token.make_token(user),
                'uid':urlsafe_base64_encode(force_bytes(user.pk)),
        })
        ....send mail
    else:
        return redirect('somewhere_else')

EDIT>:当我尝试使用电子邮件验证信息更新模型时失败了:

EDIT>: It's failing when I try to update my model with the email verification info:

if user is not None and account_activation_token.check_token(user, token):
      object, created = User_Profile.objects.get_or_create(
            user=request.user,
            ## This is where it fails
            email_validated=True,
        )

推荐答案

我认为您激活用户的方法在这里是错误的.我认为您不需要激活经过身份验证的用户.因为如果他们通过了身份验证,则意味着他们是活动用户,因此他们不需要激活.

I think your approach for activating user is wrong here. I don't think you would need to activate users who are authenticated. Because if they are authenticated, it means they are active users, hence they don't need activation.

关于错误,激活用户后,您在此处获得了用户信息:

And regarding the error, when users are activated, you got the user information here:

if user is not None and account_activation_token.check_token(user, token):  # <-- in user variable

那么为什么不在下一节中使用它(根据评论):

So why not use it in the next section(as per comments):

obj, created = User_Profile.objects.get_or_create(
    user=user,  # <-- here
    email_validated=True,
)

这篇关于尝试使用Django电子邮件确认来验证用户,但是某些用户在尝试确认其电子邮件时收到此错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆