Django反向URL不匹配模式

时间:2018-10-29 16:28:31

标签: python django

来自tests.py

url = reverse('password_change_done', args =[self.mylog.token, self.john.password])

来自urls.py

url(r'^password_change_done/$', password_change_done, name='password_change_done'),

from views.py

def password_change_done(request, token=None, new_pass=None):
print(request.method)
if request.method =="POST":
    try:
        login_obj = LoginLog.objects.get(token=token)
        trust = login_obj.trust_obj.name
        new_pass = login_obj.login_obj.password
        new_pass = request.POST.get("new_pass")

        login_obj.login_obj.password = new_pass
        login_obj.login_obj.save()

        return render(request, 'password_change_done.html', {
            "Trust": request.POST.get("Trust"),
            "email": request.POST.get("email"),

        })

来自password_change_done.html

{% extends 'base.html' %}
{% block body %}
<h2>The password for {{Trust}}, {{email}} has been updated.</h2>
<br><br>
You can now close this window.
{% endblock %}

我遇到此错误:

django.urls.exceptions.NoReverseMatch: Reverse for 'password_change_done' with arguments '('cc6f98fe-93ea-4cc2-97c5-a8b16b6308cc', 'pbkdf2_sha256$30000$zdbekeYMwQfC$WyW7FsZZg6hgWhmw6USs6etLVJlOnol6RmISFSlg1+4=')' and keyword arguments '{}' not found. 1 pattern(s) tried: ['password_change_done/$']

我认为反向模式正确使用了匹配项,为什么会出现此错误?

2 个答案:

答案 0 :(得分:0)

您的网址格式没有任何组:

url(r'^password_change_done/$', password_change_done, name='password_change_done'),

因此,您应该不带任何参数地将其反转:

url = reverse('password_change_done')

由于password_change_done视图仅显示一条消息,表明密码已更改,因此不需要任何参数。

答案 1 :(得分:0)

URL password_change_done/不需要任何其他参数。您可以像这样反转它:

url = reverse('password_change_done')

针对这种情况:

url = reverse('password_change_done', args=[self.mylog.token, self.john.password])

相应的网址格式如下:

url(r'^password_change_done/(?P<token>.+)/(?P<b64pwd>.+)/$', password_change_done, name='password_change_done'),