简体   繁体   English

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

[英]Why am I getting a Django NoReverseMatch error?

I am trying to send an activation email to users when signing up, but I keep getting a NoReverseMatch error when trying to render the URL in the template.我正在尝试在注册时向用户发送激活 email,但在尝试在模板中呈现 URL 时,我不断收到 NoReverseMatch 错误。

The error I get is:我得到的错误是:

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

urls.py网址.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
]

template.txt模板.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 %}

The pattern for the token variable is:令牌变量的模式是:

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

THis thus means that there are two sequence of [0-9A-Za-z] characters: one with one to thirteen characters, and one with one to twenty characters, and these are separated by a hyphen.因此,这意味着有两个[0-9A-Za-z]字符序列:一个有 1 到 13 个字符,另一个有 1 到 20 个字符,它们之间用连字符分隔。

Your token however has:但是,您的令牌具有:

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

So this does not match the specifications.所以这与规格不符。 If you want to accept this token, you can alter the pattern, for example to:如果你想接受这个令牌,你可以改变模式,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM