2

在我的项目中使用django.contrib.auth.views.login方法执行登录方法,如何防止/registration/login/登录成功后再次访问该页面?

4

3 回答 3

5

您可以使用此AnonymousRequired -Decorator装饰您的登录视图。

有关装饰器的一些背景信息,请参阅此博客文章,它甚至可以使用您的特定问题来解释事情:http: //passingcuriosity.com/2009/writing-view-decorators-for-django/

于 2011-04-13T15:18:48.660 回答
2

在您的登录视图中,检查用户是否已通过身份验证:

def your_login(request):
    if request.user.is_authenticated():
        return redirect('yourhomepage') # or somewhere else or last url

编辑

假设您想继续以这种方式进行身份验证,我能想到的唯一重定向方法是通过中间件。

  1. 创建中间件
  2. 检查当前 url 是否与您的登录 url 匹配
  3. 如果用户已经通过身份验证,则重定向到主页,否则照常处理
于 2011-04-13T15:43:52.607 回答
0

检查在您的登录视图中,检查用户是否已经通过身份验证,或者您应该接受它:

def your_login(request):
if request.user.is_authenticated():
    return redirect('/') #redirect to your home page

它是正确的

感谢您确认我的回答

于 2020-03-03T07:54:14.053 回答