Django 1.6 密码重置

1 投票
3 回答
1739 浏览
提问于 2025-04-18 11:43

我刚刚把我的Django安装从旧版本更新到了1.6。

我的urls.py文件里有以下内容:

url(r'^user/password/$','django.contrib.auth.views.password_change',name='password_change'),
url(r'^user/password/done/$','django.contrib.auth.views.password_change_done',name='password_change_done'),
url(r'^user/password/reset/$', 'django.contrib.auth.views.password_reset', name='password_reset'),
url(r'^user/password/password/reset/done/$', 'django.contrib.auth.views.password_reset_done', name='password_reset_done'),
url(r'^user/password/reset/complete/$', 'django.contrib.auth.views.password_reset_complete', name='password_reset_complete'),
#url(r'^user/password/reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm', name='password_reset_confirm'),
url(r'^user/password/reset/confirm/(?P<uidb36>[0-9A-Za-z_\-]+)/(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm_uidb36', name='password_reset_confirm'),

我一直在尝试让用户重置他们的密码,但是当提交电子邮件地址时,我遇到了以下错误:

NoReverseMatch: Reverse for 'django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb36': 'xxxxx', u'token': u'xxxxxxxxxxxxx'}' not found. 0 pattern(s) tried: []

(这里的x是为了问题而改的)

Django的django.contrib.auth.views.password_reset_confirm函数在这里:

# Doesn't need csrf_protect since no-one can guess the URL
@sensitive_post_parameters()
@never_cache
def password_reset_confirm(request, uidb64=None, token=None,
                           template_name='registration/password_reset_confirm.html',
                           token_generator=default_token_generator,
                           set_password_form=SetPasswordForm,
                           post_reset_redirect=None,
                           current_app=None, extra_context=None):
    """
    View that checks the hash in a password reset link and presents a
    form for entering a new password.
    """
    UserModel = get_user_model()
    assert uidb64 is not None and token is not None  # checked by URLconf
    if post_reset_redirect is None:
        post_reset_redirect = reverse('password_reset_complete')
    else:
        post_reset_redirect = resolve_url(post_reset_redirect)
    try:
        uid = urlsafe_base64_decode(uidb64)
        user = UserModel._default_manager.get(pk=uid)
    except (TypeError, ValueError, OverflowError, UserModel.DoesNotExist):
        user = None

    if user is not None and token_generator.check_token(user, token):
        validlink = True
        if request.method == 'POST':
            form = set_password_form(user, request.POST)
            if form.is_valid():
                form.save()
                return HttpResponseRedirect(post_reset_redirect)
        else:
            form = set_password_form(None)
    else:
        validlink = False
        form = None
    context = {
        'form': form,
        'validlink': validlink,
    }
    if extra_context is not None:
        context.update(extra_context)
    return TemplateResponse(request, template_name, context,
                            current_app=current_app)

当我使用注释掉的那个网址时,与下面的那个网址相比,出现了以下错误:

NoReverseMatch: Reverse for 'django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb36': 'xxxxx', u'token': u'xxxxxxxxxxxxxx'}' not found. 1 pattern(s) tried: ['user/password/reset/confirm/(?P<uidb64>[0-9A-Za-z_\\-]+)/(?P<token>.+)/$']

3 个回答

0

已编辑

为什么这一行被注释掉了?:

#url(r'^user/password/reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm', name='password_reset_confirm'),
0

我发现了这里发生了什么。

在我的模板文件夹里,有一个叫做registration/的文件夹,里面的代码只支持之前的版本。所以我用1.6.5的模板文件更新了它。

另外,不更新管理文件也导致了一些问题。

3

你在视图函数中第一个关键字参数的名字是 uidb64,但你在传递的关键字参数字典里用了 uidb36。你需要修改你的URL模式,使用注释掉的那个版本。这个方法的签名在1.6版本中已经改变了。

再看看你的错误信息,我觉得问题不在于你的URL配置,而是在你使用反向URL解析的视图或模板中。这个反向解析是通过 django.core.urlresolvers.reverse 方法或者 url 模板标签来实现的。你可能在这里传递了一个关键字参数,键是 uidb36,需要改成 uidb64

撰写回答