私は組み込みのauth_views.password_reset(_confirm, _done, _complete)
機能を使用していますが、これらのビューへのアクセスは非ログイン(匿名)ユーザーに限定したいと思います。ログインしたユーザーがパスワードをリセットするdjango anonymous_requiredカスタムデコレータがpassword_resetビューと衝突する
私は、このリンクで@login_requiredの反対が見つかりました:http://passingcuriosity.com/2009/writing-view-decorators-for-django/
デコレータはauth_views.password_reset
のために動作します。私はそれが他の3つのビューでは動作しませんいくつかの理由について、このような
url(r'^password/reset/$',
anonymous_required(auth_views.password_reset),
name='auth_password_reset'),
としての私のURLでそれを使用しています。たとえば、次のURL:
url(r'^password/reset/done/$',
anonymous_required(auth_views.password_reset_done),
name='auth_password_reset_done'),
は私に次のエラーを与える:
Exception Value:
Reverse for 'django.contrib.auth.views.password_reset_done' with arguments '()' and keyword arguments '{}' not found.
誰が、なぜ教えてもらえますか?
デコレータコード所与である:私はpassword_resetエラーメッセージに記載さルーチンの逆を含む
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)