Python django 2 re_路径表达式传递电子邮件验证令牌

Python django 2 re_路径表达式传递电子邮件验证令牌,python,django,python-3.x,django-urls,django-2.0,Python,Django,Python 3.x,Django Urls,Django 2.0,我一直坚持在Django2URL中使用正则表达式。 一切似乎都是正确的,但是我得到了404错误 实际路径: http://localhost:8000/user/activate/b'MjQ'/4zb-89a933fe70e0d12de725/ 错误: Using the URLconf defined in ec2django.urls, Django tried these URL patterns, in this order: user/ ^user/activate/

我一直坚持在Django2URL中使用正则表达式。 一切似乎都是正确的,但是我得到了
404
错误

实际路径:

http://localhost:8000/user/activate/b'MjQ'/4zb-89a933fe70e0d12de725/
错误:

Using the URLconf defined in ec2django.urls, Django tried these URL        patterns, in this order:

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

The current path, user/activate/b'MjQ'/4zb-89a933fe70e0d12de725/, didn't match any of these.
核查:

try:
    uid = force_text(urlsafe_base64_decode(uidb64))
    user = MyUser.objects.get(pk=uid)
except(TypeError, ValueError, OverflowError, MyUser.DoesNotExist):
    user = None
if user is not None and default_token_generator.check_token(user, token):
    user.is_active = True
    user.save()

url或令牌格式有问题吗?感谢您的帮助。

您的url包含单引号(
b'MjQ'
)作为第一个模式的参数,它与regex
[0-9A-Za-z\-]
不匹配。那么uid应该如何传输呢?我发现的大多数样本都以这种方式传递
uid
,模式如下所示。在本例中,它是python代码,它属于python脚本,而不是模板中呈现的url。如何在模板中对URL进行编码?回答:
            from django.utils.encoding import force_bytes
            from django.utils.http import urlsafe_base64_encode
            from django.template.loader import render_to_string
            from django.contrib.auth.tokens import default_token_generator

            message = render_to_string('email_account_activate.html', {
                'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'token': default_token_generator.make_token(user),
            })
try:
    uid = force_text(urlsafe_base64_decode(uidb64))
    user = MyUser.objects.get(pk=uid)
except(TypeError, ValueError, OverflowError, MyUser.DoesNotExist):
    user = None
if user is not None and default_token_generator.check_token(user, token):
    user.is_active = True
    user.save()