r25*_*971 16 python django django-templates python-3.x django-2.0
我无法理解为什么我的代码不起作用.在它运行之前,但现在,当我运行服务器并进行测试时,代码不起作用.
当用户注册时,我发送激活邮件,如下所示:
def send_activation_email(serializer, request, user):
current_site = get_current_site(request)
message = render_to_string('acc_active_email.html', {
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': account_activation_token.make_token(user),
})
mail_subject = 'Activate your blog account.'
to_email = serializer.data['email']
email = EmailMessage(mail_subject, message, to=[to_email])
email.send()
Run Code Online (Sandbox Code Playgroud)
acc_active_email.html
{% autoescape off %}
Hi {{ user.username }},
Please click on the link to confirm your registration,
http://{{ domain }}{% url 'activate' uidb64=uid token=token %}
{% endautoescape %}
Run Code Online (Sandbox Code Playgroud)
和我的网址文件
.
.
url(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
views.activate_account, name='activate'),
.
.
Run Code Online (Sandbox Code Playgroud)
但我有这个错误:
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'activate' with keyword arguments '{'uidb64': b'NDM', 'token': '4qz-8f770502bd8b02786da9'}' not found. 1 pattern(s) tried: ['activate/(?P<uidb64>[0-9A-Za-z_\\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$']
Run Code Online (Sandbox Code Playgroud)
突出这一行 http://{{ domain }}{% url 'activate' uidb64=uid token=token %}
Ala*_*air 45
在Django 2.0中,您应该decode()在base64编码后调用uid,将其转换为字符串:
message = render_to_string('acc_active_email.html', {
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
'token': account_activation_token.make_token(user),
})
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅Django 2.0发行说明中的注释.
对于较新版本的 Django,您可以使用 slug 语法。例如:
path(
'activate/<slug:uidb64>/<slug:token>/',
views.activate_account,
name='activate'
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5695 次 |
| 最近记录: |