grilam14
grilam14

Reputation: 47

Trouble with return redirect() in flask python

I think the solution should be:

return redirect(url_for(main))

as my terminal tells me I am moving to '/' like I should be, but it doesn't work. I can get

127.0.0.1 - - [23/Apr/2018 23:01:36] "GET / HTTP/1.1" 200 -

It just doesn't actually take me there.

There has to be a really dumb simple error here. All I want to do is, upon storing the new username and password into the database, return to the home screen, which is listed at '/'. I basically have this exact process in another page, so I don't really see the problem. This is just a crude sign up page to satisfy the requirement for a project, it's more symbolic than anything.

I can also confirm the entry does make it to the database and "here" gets printed.

I feel like

return redirect('/') 

is a valid line.

https://www.tutorialspoint.com/flask/flask_redirect_and_errors.htm

@app.route('/signUp',methods=['POST','GET'])
def signUp():

    conn = mysql.connect()
    cur = conn.cursor()

    try:
        _name = request.form['inputName']
        _email = request.form['inputEmail']
        _password = request.form['inputPassword']

        if _name and _email and _password:

            cur.callproc('sp_createUser',(_name,_email,_password,))
            print ("Registered")
            data = cur.fetchall()

            conn.commit()
            json.dumps({'message':'User created successfully !'})
            print('here')
            return redirect('/')#not redirecting to home

        else:
            return json.dumps({'html':'<span>Enter the required fields</span>'})


    except Exception as e:
        return json.dumps({'error':str(e)})
    finally:
        cur.close() 
        conn.close()


@app.route("/")
def main():
     return(render_template('Senty.html'))

I'll also post terminal results just in case

127.0.0.1 - - [23/Apr/2018 21:45:06] "GET /showSignUp HTTP/1.1" 200 -
Registered
here
127.0.0.1 - - [23/Apr/2018 21:45:14] "POST /signUp HTTP/1.1" 200 -

Upvotes: 0

Views: 759

Answers (4)

Mohammed Alhussaini
Mohammed Alhussaini

Reputation: 99

Solution 1:

You could directly use return(render_template('Senty.html')) in your code like so:

@app.route('/signUp',methods=['POST','GET'])
def signUp():

    conn = mysql.connect()
    cur = conn.cursor()

    try:
        ...
        ...

        return(render_template('Senty.html'))


        else:
            ...

    except Exception as e:
        ...
    finally:
        ...

Solution 2:

Or you could use return redirect(url_for('main')) in the code above instead.

This is basically a summary of the correct answers before mine.

Upvotes: 0

Mikhail Sidorov
Mikhail Sidorov

Reputation: 1434

You have a syntax error in your "/" route

@app.route("/")
def main():
     return(render_template('Senty.html'))

The correct code is:

return render_template('Senty.html')

Upvotes: 0

mckuok
mckuok

Reputation: 754

return redirect(url_for('signUp')) I believe it should be the name of the function.

Upvotes: 1

Mikhail Sidorov
Mikhail Sidorov

Reputation: 1434

Probably you need to use:

return redirect(url_for('/'))

Upvotes: 0

Related Questions