The problem of too many redirect times when setting login middleware under the Django framework

Project scenario:

We don't want users to jump to the background operation page, so we need to judge the user's jump and set up a middleware. If not logged in, redirect to login page

Problem Description

After setting up the middleware, users accessing the background path are forced to jump to the login page, and the page has too many redirects.

Cause Analysis:

There is a problem of too many redirects. When logging in, I find that you are not logged in, and then jump to the login page. Since the route of my login interface is myadmin/login, which already contains myadmin, I will jump after finding that you are not logged in. The route
has become myadmin/myadmin/login. Why do I add an extra myadmin before my route? Because you set path("myadmin/",include("myadmin.urls")) in the general route, which means All sub-routes under myadminAPP are preceded by myadmin
Insert image description here

solution:

In the sub-routing, set the routing of the login page to login.
Insert image description here

Guess you like

Origin blog.csdn.net/qq_33655643/article/details/123907208