Redirect after login to web2py

I'm having trouble managing redirection after logging in to web2py.

According to this , web2py handles redirection after login in different ways, depending on whether the login was initiated by the system (for example, when accessing authorization is a protected function) or by the user (when you click on the "login" link). In the first case, the behavior is to redirect to the link page after logging in, as expected. In the latter, however, the user is redirected to the index page after logging in or to the page encoded in the auth.settings.login_next file.

How can I set up that redirection after login always returns you to the link page, no matter how the login was initiated?

+4
source share
1 answer

You can change your "Login" so that they always include the current URL as the value of the _next variable in the query string. For example, wherever you create a login link, define it as follows:

 A('Login', _href=URL('default', 'user', args='login', vars=dict(_next=URL(args=request.args, vars=request.vars)))) 

This will add the URL of the current page (including any arguments and vars) to the _next variable in the query string of the login link, which will lead to a redirect to the current page after login.

If you use the auth.navbar() helper to create your login account, there is already a change in the trunk that solves this problem (will be released soon). The new auth.navbar() will automatically add the _next variable to all links, so after clicking any navigation link, the user will be redirected back to the original page. In the meantime, you can edit auth.navbar() as follows:

 import urllib navbar = auth.navbar() if not auth.is_logged_in and request.args(0) != 'login': navbar[1]['_href'] += '?_next=' +\ urllib.quote(URL(args=request.args, vars=request.vars)) 
+5
source

Source: https://habr.com/ru/post/1384362/


All Articles