Django-如何获取电子邮件链接以在移动设备上工作?

问题描述 投票:0回答:1

我的Django网站发送了一封电子邮件确认链接,并且在桌面上一切正常。但是,在移动设备上,链接文本以蓝色突出显示并带有下划线(即看起来像链接),但是当用户单击链接时什么也没有发生。它应打开浏览器标签,并说“您已确认电子邮件等”

感谢任何提示!

views.py:

def signup(request):
    if request.method == 'POST':
        #send signup form
        email_address = request.POST['email']
        if request.POST['password1'] == request.POST['password2']:
            try:
                user = User.objects.get(email=email_address)
                return render(request, 'accounts/signup.html', {'error': "Email already in use."})
            except User.DoesNotExist:
                user = User.objects.create_user(request.POST['email'], password=request.POST['password1'])
                #auth.login(request, user)
                #send email confirmation link then redirect:
                #user = User.objects.get(email=email_address)
                current_site = get_current_site(request)
                mail_subject = 'Welcome to My Site'
                plain_msg = 'Thank you for signing up to My Site! Click this link to activate your account:\n\n'+current_site.domain+'/accounts/activated/'+urlsafe_base64_encode(force_bytes(user.pk)).decode()+'/'+account_activation_token.make_token(user)+'/'
                msg = '<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>title</title></head><body>Confirm your email address to continue:<br/><a href="'+current_site.domain+'/accounts/activated/'+urlsafe_base64_encode(force_bytes(user.pk)).decode()+'/'+account_activation_token.make_token(user)+'/">Confirm my email address</a></body></html>'
                print(msg)
                send_mail(mail_subject, plain_msg, '[email protected]', [email_address], html_message=msg)
                return render(request, 'accounts/activation-link-sent.html', {'email': email_address})
    #truncated for Stack Overflow post
python django html-email
1个回答
1
投票

您应使用build_absolute_uri创建包含当前域和协议的标准链接,然后在您的电子邮件中使用此链接

link = request.build_absolute_uri('/accounts/activated/'+urlsafe_base64_encode(force_bytes(user.pk)).decode()+'/'+account_activation_token.make_token(user)+'/')

您是否有与此网址匹配的网址格式?您应该考虑使用reverse建立网址

reverse
© www.soinside.com 2019 - 2024. All rights reserved.