Django: Redirect logged in users from login page

前端 未结 9 2416
醉酒成梦
醉酒成梦 2020-11-29 18:02

I want to set up my site so that if a user hits the /login page and they are already logged in, it will redirect them to the homepage. If they are not logged in

9条回答
  •  悲哀的现实
    2020-11-29 18:30

    Add this decorator above your login view to redirect to /home if a user is already logged in

    @user_passes_test(lambda user: not user.username, login_url='/home', redirect_field_name=None)

    and don't forget to import the decorator

    from django.contrib.auth.decorators import user_passes_test

提交回复
热议问题