Implementation method of user defined jump page after login in Django framework

  • 2021-07-22 10:46:42
  • OfStack

After Django auth login, the page will jump to/account/profile, and the modification will jump to other pages

In the past few days, I have been learning django. django is very powerful. The self-contained auth can basically meet the needs of user registration, login and logout. A simple user registration and login system using django auth is enough. Of course, it is not available. It is also very simple to write the template of the landing page by yourself. I tried it for 1 time and found a problem. When the login is verified successfully, the page will automatically jump to/account/profile. I don't want to jump the page to this page. How to modify it?

After checking a pile of web pages, it is still reliable in official website. It is very simple. Just add a sentence to setting of project and Okay!


LOGIN_REDIRECT_URL =  ' /index'

I here is designated to jump to the index page after landing successfully, friends can modify according to their own needs. That's okay! Cheer, and then go on to solve other problems!

========================================================================================================================================================

Django jump and redirect methods:

A common scenario for jumping and redirecting is to log in and log out and return to the current page. Let me give you an example of logging in and logging out.

For example, when a user is browsing an article and finds that downloading the attachment of the article needs to log in. At this time, click the login link to go to the login page. After entering the user name and password to log in successfully, it will automatically go back to the page where the original article is located. (At present, many websites use ajax method to pop up dialog boxes to log in, and the effect is better.)

How to do it? The following is the implementation of django, but most web framework can do this.

Logout operation:

Because there is no need for a separate logout page, it is relatively simple to use the logout page of HTTP_REFERER and Django as follows:


def logout_user(request):
 logout(request)
 return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))

Login action:

Login operation is relatively complicated, because 1 generally has a separate login page. If the login is successful, taking HTTP_REFERER is the login page's own url, instead of the previous page.

You can use session of django. When you go to login, you use get request of the page. At this time, record HTTP_REFERER in session. When the user enters the user name and password for login (Post request), you can take it out of session:


def login_user(request):
 if request.method == 'GET':
  # Remember the source url If not, it is set to the home page ('/')
  request.session['login_from'] = request.META.get('HTTP_REFERER', '/')
  #TODO: Displays the landing page, blablabla
 elif request.method == 'POST':
  #TODO:  User login operation, blablabla
  # To redirect to the source url
  return HttpResponseRedirect(request.session['login_from'])

The final redirection sometimes requires the user to feel redirected back, just write a simple page to tell the user to jump after two seconds, and then change the corresponding login code:


<p> After two seconds, go to the following link. If there is no response for a long time, please click directly: </p>
<p><a href="{{ refresh_url }}" rel="external nofollow" >{{ refresh_url }}</a></p>
<meta http-equiv="refresh" content="2;url={{ refresh_url }}">

Summarize


Related articles: