【问题标题】:Django template url NoReverseMatch for re_pathre_path 的 Django 模板 url NoReverseMatch
【发布时间】:2018-12-17 19:56:32
【问题描述】:

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

这是模板中的代码:

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

这是我的网址格式:

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 的参数!

【问题讨论】:

    标签: django django-templates django-urls django-users


    【解决方案1】:

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

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

    【讨论】:

    • 虽然这并没有完全提供我需要的答案,但它帮助我提出了正确的问题并且是正确的(我只需要一些稍微不同的代码)。原来我的问题是 Django 2.0 的问题。这是我能找到的完整答案stackoverflow.com/questions/47177696/…
    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 2023-03-10
    • 2014-07-17
    • 2017-05-24
    • 1970-01-01
    • 2012-10-10
    • 2015-01-03
    • 2020-02-10
    相关资源
    最近更新 更多