Django 电子邮件确认:单独打开电子邮件会触发用户更新,他们甚至不必单击链接

问题描述

我设置了电子邮件确认,以便当用户注册时,他们会将 is_active 设置为 true,但我的自定义 email_confirmed 字段将为 false。要将其更新为 true,他们必须单击注册时发送给他们的电子邮件。这是电子邮件的样子:

enter image description here

奇怪的是,只要我打开这封电子邮件,甚至没有悬停或单击链接,我就可以在 Heroku 日志中看到 activate() 中的 views.py 函数(如下所示)是开火!我什至不必单击链接。这导致了问题,有什么方法可以阻止仅从打开电子邮件的人触发该功能?最好我想要一个纯文本选项,因为并非所有电子邮件提供商都能正确显示 html 电子邮件

views.py 函数在点击电子邮件中的链接调用

#View called from activation email. Activate user if link didn't expire (1 week). Offer to resend link if the first expired
def activation(request,key): # key is the pk accepted in the url from clicking the link in the email. User may/may not be logged in... Actually maybe i will require login
    print('~~~in activation()~~~')
    user = User.objects.filter(activation_key=key).first()

    if user is None: # No user found with that activation_key. Assume they clicked an old link and bring them to ResendEmailConfirmationForm
        if not request.user.is_authenticated:
            messages.add_message(request,30,'Your email confirmation link has expired. Login then enter your email to send a new one.')
        return redirect('new-activation-link')

    print('user.email_confirmed: ' + str(user.email_confirmed))

    if user.email_confirmed:
        print('~~~in user.email_confirmed (97)~~~')
        messages.success(request,f'Your email has been confirmed.')
        return redirect('blog-home')


    else: # Email not confirmed. User clicked link in email
        if timezone.Now() > user.key_expires: # activation_key Expired
            return new_activation_link(request) # take user to form which sends a new email activation

        else: # Activation successful
            print('~~~in # Activation successful (107) ~~~')
            user.email_confirmed = True
            user.save()
            messages.success(request,f'Your email has been confirmed.')
            if request.user.is_authenticated:
                return redirect('blog-home')  # bring to home page if logged in
            else:
                return redirect('login')

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)