尝试使本教程在我的应用中起作用: 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()
我的激活视图与本教程中的视图完全相同
答案 0 :(得分:1)
由于Django >2.2
,urlsafe_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 %}
有两种情况可能导致此问题:
path('activate/<uidb64>/<token>/', views.activate, 'activate'),
您应该以此命名您的视图:
path('activate/<uidb64>/<token>/', views.activate, name='activate'),
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 %}
希望有帮助!