简体   繁体   中英

login_required decorator not redirecting to the following view

I want to render the specified template only when the admin got logged in so I used login_required decorator but it is redirecting to admin rather than rendering and displaying the specified template

@login_required(login_url='/admin/')
def employeesignupview(request):
    return render(request,'todoapp/employeesignup.html')

Loginrequired means that you only want people login to have access to your page but here the page you are protecting is the signup page that's astonishing.

And one more thing the login_url redirect you to the url you pass in your case you put "/admin/" so the view will send you to the admin page if you are not login.

When it redirects the user to '/admin/' the first time, it creates a query param( next ) with the URL of your view( employeesignupview ) to be able to navigate the user back after successful login. But to access '/admin/' URL, the user needs to be logged in. And because the user isn't logged in to access '/admin/' this page it redirects to the admin login page and overriding the existing next param to /admin/ value and that's why after logging in the user is being redirected to the /admin/ page. To make it work as you wish, simply replace login_url parameter value to this '/admin/login/' :

@login_required(login_url='/admin/login/')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM