尝试使电子邮件激活正常工作,但失败

时间:2019-09-29 01:06:14

标签: django django-email

尝试使本教程在我的应用中起作用: https://medium.com/@frfahim/django-registration-with-confirmation-email-bb5da011e4ef

无论是否包含.decode(),“ uid”都会失败。

Arrays.sort

这是两个错误:

message = render_to_string('premium/activation_email.html', {'user':user,

 'token': account_activation_token.make_token(user),
 #this fails both with the .decode() and without
 'uid':urlsafe_base64_encode(force_bytes(user.pk)).decode(),
            })
mail_subject = 'Activate your membership account.'
send_mail(mail_subject, message,'info@mysite.com', [request.user.email])

然后,如果我添加.decode():

Reverse for 'activate' not found. 'activate' is not a valid view function or pattern name

这是我的带有激活标记的urls.py:

str object has no attribute decode()

我的激活视图与本教程中的视图完全相同

1 个答案:

答案 0 :(得分:1)

由于Django >2.2urlsafe_base64_encode将返回字符串而不是字节字符串,因此您不必在.decode()之后再调用urlsafe_base64_encode

  

在Django 2.2中已更改:   在旧版本中,它返回一个字节字符串而不是字符串。

按照您在问题中嵌入的准则,问题Reverse for 'activate' not found就是这样的:

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

有两种情况可能导致此问题:

  1. 您的路径:
path('activate/<uidb64>/<token>/', views.activate, 'activate'),

您应该以此命名您的视图:

path('activate/<uidb64>/<token>/', views.activate, name='activate'),
  1. 如果您的视图停留在站点级别(在Django应用程序URL内,而不在ROOT_URLS内),则可能需要在app_name = 'your_app_name'urlpatterns的顶部添加urls.py。然后在您的邮件模板中:
{% autoescape off %}
Hi {{ user.username }},
Please click on the link to confirm your registration,
http://{{ domain }}{% url 'your_app_name:activate' uidb64=uid token=token %}
{% endautoescape %}

希望有帮助!

相关问题