我正在使用内置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)