Django无法识别用户表单电子邮件验证链接:

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

抱歉,我已经问了很多遍了,但是我上个星期花了20个小时,无法弄清楚。是的,我是Django新手。这是我的第一个应用程序。

我已经在shell中测试了'urlsafe_base64_encode(force_bytes(user.pk))',它可以通过用户的'uid',但是在应用程序本身中不起作用。即使链接传递了正确的主键,我仍然知道所有链接的“激活链接无效”,我不确定出了什么问题。请帮忙。

views.py/注册页面

  from_email, subject = 'domain <info@domain>', 'Email verification'
  htmly = get_template('activation_email_template.html')
  username = user.username
  message = render_to_string('activation_email.html', {
                    'user':user,
                    'token':account_activation_token.make_token(user),
                    'uid':urlsafe_base64_encode(force_bytes(user.pk)),
       })
  d = {'username': username, 'url':reverse('activate',
                kwargs={
                'token':account_activation_token.make_token(user),
                'uidb64':urlsafe_base64_encode(force_bytes(user.uid))
                })}
  html_content = htmly.render(d) 
  msg = EmailMultiAlternatives(subject, message, from_email,[user.email])
  msg.attach_alternative(html_content, "text/html")
  msg.send()

这是我的activate / views.py:

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):
       ...
    else:
        return HttpResponse('Activation link is invalid!')

由于某种原因,每次尝试此操作时,都会收到“激活链接无效”。我知道它发送的是正确的PK,因为我可以在该URL上叮当响,并且该URL与正确编码的PK是正确的。但是,激活视图无法识别此pk。

这是我的activation_email.html:

{% autoescape off %}
Hi {{ user.username }},
Thanks for creating an account. Please click on the link to confirm your registration,
http://domain{% url 'activate' uidb64=uid token=token %}
{% endautoescape %}

这是激活电子邮件模板:

<h3>Hi <strong>{{ username }}</strong>,</h3>
<p>Thanks for creating an account. Please click on the following link to confirm your registration:</p>
<a style="background-color: #f79407 ;padding: 10px 15px; text-decoration: none;color: #ffffff;text-transform: uppercase;font-size: 15px;" href="https://domain{{ url }}">Email Activation</a>

请帮助!

html django
1个回答
0
投票

尝试一下:

from django.contrib.sites.shortcuts import get_current_site
site = get_current_site(request)
message = render_to_string('activation_email.html', {
                    'user':user,
                     'domain':site.domain,
                    'token':account_activation_token.make_token(user),
                    'uid':urlsafe_base64_encode(force_bytes(user.pk)).decode(),

在激活链接中:

http://{{domain}}{% url 'activate' uidb64=uid token=token %}
© www.soinside.com 2019 - 2024. All rights reserved.