In Web2py, how to redirect to another page after user login?

63 views
Skip to first unread message

Anusha Narayan

unread,
Jul 11, 2020, 2:44:58 AM7/11/20
to web2py-users

By default, after a successful login the page directs to "default/index". I want it to redirect to some other page.

This my controller code "user.py"

def login():
    form=auth.login()
    if form.process().accepted:
        redirect(URL('page_1'))
    return dict(form=form)
This is the view code for "user/login":
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="container">
        
        <div class="form-container">
     {{=form.custom.begin}}


            <input type="text" name="email" placeholder="email id" class="string" id="auth_user_email" value="" />

        <input placeholder="Password" name="password" class="password" id="auth_user_password" type="password" class="field" value="" />
            <div class="wrap">
                <input name="submit" type="submit" class="btn" value="{{=T('Login')}}" />
                <input name="sign_up" class="btn" type="submit" formaction='/{{=request.application}}/default/user/register' value="{{=T('Sign Up')}}"/>
            </div>
            <input name="Forgot Password" type="submit" class="btn" value="{{=T('Forgot Password')}}" formaction='/{{=request.application}}/default/user/retrieve_password' />
            {{=form.custom.end}}
        </div>
    </div>
    
</body>
</html>
The thing is, when the form is getting processed, it is still redirecting to the page "default/index". I don't know where to change this default setting in the application. The code of my controller is not working properly.

jonatha...@whatho.net

unread,
Jul 11, 2020, 5:39:16 AM7/11/20
to web2py-users
Why are you writing your own login function? What is wrong with the one in Auth (http://web2py.com/books/default/chapter/29/09/access-control#)?

When I have used the Auth facilities, and I want to restrict access to a page, I use the decorater @auth.requires_login() before my controller function (say index()). When a user tries to visit index, they are routed to a login page, after login they are routed to index(), the controller they were trying to visit in the first place.

Sorry if you already know all this which is documented in the link above.
Reply all
Reply to author
Forward
0 new messages