python - Django 2.0 与关键字参数 uidb64 的 NoReverseMatch

标签 python django python-3.x django-templates 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()

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 %}

和我的网址文件

.
.
    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'),
.
.

但是我有这个错误:

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})/$']

突出显示此行http://{{ domain }}{% url 'activate' uidb64=uid token=token %}

最佳答案

在 Django 2.0 和 2.1 中,您应该在对 uid 进行 Base64 编码后调用 decode(),将其转换为字符串:

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),
})

请参阅 Django 2.0 release notes 中的注释了解更多信息。

在 Django 2.2+ 中,urlsafe_base64_encode returns a string ,所以不需要解码。

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),
})

使用 force_text 应该可以编写与 Django <= 1.11、2.0-2.1 和 2.2+ 兼容的代码。请注意,以下内容未经测试。

from django.utils.encoding import force_text

message = render_to_string('acc_active_email.html', {
    'user': user,
    'domain': current_site.domain,
    'uid': force_text(urlsafe_base64_encode(force_bytes(user.pk))),
    'token': account_activation_token.make_token(user),
})

一旦放弃对 Django < 2.2 的支持,您就可以放弃 force_text 并使用第二个代码片段。

关于python - Django 2.0 与关键字参数 uidb64 的 NoReverseMatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58173224/

相关文章:

python - 由于 Win32 模块,PyInstaller 无法正常工作

python - 鹡鸰 block : accessing context and request in overridden get_context

python - HoughCircles的'NoneType'对象不是下标错误

python - 如何将元素从另一个列表添加到列表?

python - 无法使用正确的用户名和密码登录 Django 管理员

python - 什么是 _PyEval_EvalFrameDefault?

python - Python3 变量名称的简单区别能否改变代码的运行方式?

python - 属性错误: object has no attribute 'listbox' ?

python - 排序技术 Python

python - 本地文件系统作为 Django 中的远程存储