How can I add admin authentication to all pages?

I already have implemented admin login authentications to site. Whenever admin visits dashboard he is asked to put a log in credentials.

But when you visit some page from admin panel lets say .../admin/addblog.php in such cases it doesn't ask for authentication.

How can I redirect to the admin login page and once authenticated then direct back to the origional page again

1 answer

  • answered 2019-05-18 05:47 AriG

    There is a simple way to do this, just add the "login_url" parameter to @login_required and if the user is not login he will be redirected to the login page. You can find it here

    from django.contrib.auth.decorators import login_required
    
    @login_required(login_url='/accounts/login/')
    def my_view(request):
        ...