【问题标题】:NoReverseMatch with keyword argument uidb64 with Django 2.0NoReverseMatch 与 Django 2.0 的关键字参数 uidb64
【发布时间】:2018-04-21 00:30:57
【问题描述】:

我不明白为什么我的代码不起作用。在它工作之前,但现在,当我运行服务器并测试时,代码不起作用。

当用户注册时,我给他发送激活邮件,如下所示:

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

【问题讨论】:

  • 我想知道它是否不起作用,因为uid 是字节。尝试硬编码'uid': 'NDM' 看看它是否修复了错误。
  • @Alasdair 哇,如果我在方法 send_activation_email 中设置 'uid': 'NDM' 就可以了。这很奇怪
  • 好的,看来您需要将字节转换为字符串才能反转 url。

标签: python django python-3.x django-templates django-2.0


【解决方案1】:

在 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_encodereturns 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

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 force_text 并使用第二个代码 sn-p。

【讨论】:

    【解决方案2】:

    对于较新版本的 Django,您可以使用 slug 语法。例如:

    path(
        'activate/<slug:uidb64>/<slug:token>/',
        views.activate_account, 
        name='activate'
    )
    

    【讨论】:

      【解决方案3】:

      也许它很愚蠢,因为 URL 不是动态的,但它可以工作......

      path('activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', views.activate, name='activate'),
      

      【讨论】:

      • 请详细说明您的答案,以便于理解。
      猜你喜欢
      • 2012-12-11
      • 2013-08-06
      • 2023-03-15
      • 2013-10-11
      • 2021-10-02
      • 2019-07-21
      相关资源
      最近更新 更多