Django 模板 url re_path 的 NoReverseMatch

Django template url NoReverseMatch for re_path

我正在尝试验证 link 电子邮件作为我在 Django 中注册用户的一部分。我的模板中有一个 {% url %} 标签,但是它给我一个关于 uid 和令牌变量的 NoReverseMatch 错误。

模板中的代码如下:

https://{{ domain }}{% url 'users:activate' uidb64=uid token=token%}

这是我的 URL 模式:

re_path(r'^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'),

最后是我的错误:

django.urls.exceptions.NoReverseMatch: Reverse for 'activate' with keyword arguments '{'uidb64': b'MzY', 'token': '4xq-eb603ee3a9676d7b3edc'}' not found. 1 pattern(s) tried: ['users\/activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$']

我有一种预感,这可能与正则表达式有关,但这不是我的强项(目前)!

它似乎找到了激活 URL 模式,但我似乎无法让它识别 url 的参数!

看起来 uid 类型是字节而不是字符串。在将它传递给模板上下文之前尝试对其进行解码:

# you view code
uid = uid.decode()
context['uid'] = uid
return render(requet, 'template.html', context)