在我的项目中使用django.contrib.auth.views.login
方法执行登录方法,成功登录后如何防止再次访问/registration/login/
页面?
您可以使用此AnonymousRequired装饰器整理您的登录视图。
请参阅此博客文章以获取有关装饰器的一些背景信息,甚至可以使用您的特定问题来解释事情:http://passingcuriosity.com/2009/writing-view-decorators-for-django/
在您的登录视图中,检查用户是否已经通过身份验证:
def your_login(request):
if request.user.is_authenticated():
return redirect('yourhomepage') # or somewhere else or last url
编辑
假设您想继续以这种方式进行身份验证,我想到重定向的唯一方法是通过中间件。
用于检查,请在您的登录视图中,检查用户是否已通过身份验证,否则,您不应该使用它:
def your_login(request):
if request.user.is_authenticated():
return redirect('/') #redirect to your home page
是正确的
感谢您确认我的回答