django anonymous_required自定义装饰器与password_reset视图冲突

时间:2011-04-17 16:24:47

标签: django passwords django-views decorator password-recovery

我正在使用内置的auth_views.password_reset(_confirm, _done, _complete)功能,我想将这些视图的访问权限仅限于非登录(匿名)用户,因为登录时没有意义用户重置密码。

我在此链接找到了与@login_required相反的内容:http://passingcuriosity.com/2009/writing-view-decorators-for-django/

装饰器适用于auth_views.password_reset。我在我的网址中使用它

 url(r'^password/reset/$',
                       anonymous_required(auth_views.password_reset),
                       name='auth_password_reset'),

由于某种原因,它不能与其他3个视图一起使用。例如以下网址:

url(r'^password/reset/done/$',
                       anonymous_required(auth_views.password_reset_done),
                       name='auth_password_reset_done'),

给了我以下错误:

  

例外值:
  反向'django.contrib.auth.views.password_reset_done',参数'()'和关键字参数'{}'未找到。

谁能告诉我为什么?

给出的装饰代码是:

def anonymous_required(function=None, home_url=None, redirect_field_name=None):
"""Check that the user is NOT logged in.

This decorator ensures that the view functions it is called on can be
accessed only by anonymous users. When an authenticated user accesses
such a protected view, they are redirected to the address specified in
the field named in `next_field` or, lacking such a value, the URL in
`home_url`, or the `USER_HOME_URL` setting.
"""
if home_url is None:
    home_url = settings.USER_HOME_URL

def _dec(view_func):
    def _view(request, *args, **kwargs):
        if request.user.is_authenticated():
            url = None
            if redirect_field_name and redirect_field_name in request.REQUEST:
                url = request.REQUEST[redirect_field_name]
            if not url:
                url = home_url
            if not url:
                url = "/"
            return HttpResponseRedirect(url)
        else:
            return view_func(request, *args, **kwargs)

    _view.__name__ = view_func.__name__
    _view.__dict__ = view_func.__dict__
    _view.__doc__ = view_func.__doc__

    return _view

if function is None:
    return _dec
else:
    return _dec(function)

1 个答案:

答案 0 :(得分:0)

我看到password_reset包含错误消息中提到的例程的反转。它是否正在尝试执行此操作但是您已覆盖它,或者未将其包含在urls.conf中?

django / contrib / auth / views.py:141-142(v1.3)

if post_reset_redirect is None:
    post_reset_redirect = reverse('django.contrib.auth.views.password_reset_done')