为什么我会收到 Django NoReverseMatch 错误?

杰罗恩·范德默韦

我在注册时尝试向用户发送激活电子邮件,但在尝试在模板中呈现 URL 时,我不断收到 NoReverseMatch 错误。

我得到的错误是:

Reverse for 'user_activation' with keyword arguments '{'uidb64': 'MiC', 'token': 'aeue9g-3a31bdff969c41c0bb1d3775a1fa98ac'}' not found. 1 pattern(s) tried: ['account/activate/(?P<uidb64>[0-9A-Za-z_\\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$']

网址.py

from .views import *
from django.urls import re_path

urlpatterns = [
    re_path(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', user_activation_view, name='user_activation'), # user activation view
]

模板.txt

{% autoescape off %}
To activate your account ({{ user.email }}), please click the link below:

{{ protocol }}://{{ domain }}{% url 'user_activation' uidb64=uid token=token %}

If clicking the link above doesn't work, you can copy and paste the URL in a new browser
window instead.

If you did not make this request, please ignore this email.
{% endautoescape %}
威廉·范·翁塞姆

令牌变量的模式是:

<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20}

因此,这意味着有两种[0-9A-Za-z]字符序列:一种是 1 到 13 个字符,另一种是 1 到 20 个字符,它们之间用连字符分隔。

但是,您的令牌具有:

'aeue9g-3a31bdff969c41c0bb1d3775a1fa98ac'
\__ __/ \_____________ ________________/
   v                  v
 6 chars           32 chars

所以这与规格不符。如果你想接受这个令牌,你可以改变模式,例如:

<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,32}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章