【问题标题】:Reverse Not Found Error反向未找到错误
【发布时间】:2014-01-08 13:42:03
【问题描述】:

如果我有这样的网址:

url(r'^reset/(?P<uid>\w+)/(?P<token>\w+)/$', 'django.contrib.auth.views.password_reset_confirm', name="reset_password")

和这样的 URL 标记:

{% url 'reset_password' uid=uid token=token %}

当我尝试渲染包含标签的页面时为什么会出现此错误:

Reverse for 'reset_password' with arguments '()' and keyword arguments not found 

uid 和 token 都是有效的字符串。

【问题讨论】:

  • 你的 Django 版本是多少?
  • 尝试在 shell 中使用from django.core.urlresolvers import reverse 进行调试,然后使用reverse('reset_password', kwargs={'uid':uid, 'token':token}) 看看您是否获得了更多信息。此外,如果您的 url conf 是应用程序的一部分,请确保它包含在主 url conf 中。
  • 您确定模板上下文中的uidtoken 变量中有有效值吗?
  • 我的猜测是上下文中不存在 token 和/或 uid 变量,尝试显示它们,将其添加到同一模板的其他位置 {{token|pprint}} {{uid|pprint}}
  • 可以输出uuid和token的格式吗?他们真的在正则表达式中匹配吗?如果模式与参数不匹配,通常会发生此错误。

标签: python django django-templates django-urls django-1.5


【解决方案1】:

我会说您的 uid 或令牌具有非字母数字字符,例如“-”或“。”所以我会尝试将 urls.py 更改为:

url(r'^reset/(?P<uid>.+)/(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm', name="reset_password")

我不喜欢使用 .在正则表达式中,但如果您没有其他选择...

【讨论】:

    【解决方案2】:

    尝试以下方法:

    1.总是尝试将您的网址命名为'app_name:reset_password',只需添加即可

    app_name='your app name' 在你的 urls.py 文件的开头,如果你在同一个项目中处理多个应用程序,当你的 html 的名称同时发生冲突时,这将有助于你

    2.try this changes in your urls , as you are using django auth views and its password reset function you need to specify redirect url also after reset your password for that just add

    {'post_reset_redirect':'<app_name>:<url_name>'}
    
    url(r'^reset/(?P<uid>[-\w]+)/(?P<token>[-\w]+)/$', 'django.contrib.auth.views.password_reset_confirm', {'post_reset_redirect':'<your redirection url after reset password'},name="reset_password")}
    

    是的,请检查变量也像'uid',因为它是django自己的函数,可能是它自己的变量名,所以试试'uidb64'

    【讨论】:

      猜你喜欢
      • 2020-12-19
      • 1970-01-01
      • 2021-04-13
      • 1970-01-01
      • 2020-09-09
      • 2019-04-09
      • 2019-10-19
      • 2018-06-28
      • 1970-01-01
      相关资源
      最近更新 更多