Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

request.args.get('key') gives NULL - Flask

Tags:

python

flask

I am trying to pass the variable 'email' from the 'signup' method in my view to the 'character' method. However,

request.args.get('email') 

is saving NULL into the database. I cannot figure out why.

Here is what shows up after passing the 'email' variable to '/character':

http://127.0.0.1:5000/character?email=test%40test.com

Here is my code in 'views.py':

@app.route('/signup', methods=['GET','POST'])
def signup():
    if request.method == 'GET':
        return render_template('signup.html')
    email = request.form['email']
    return redirect(url_for('character', email=email))

@app.route('/character', methods=['GET', 'POST'])
def character():
    if request.method == 'GET':     
        return render_template('character.html')

    email = request.args.get('email')

    password = request.form['password']
    name = request.form['username']
    temp = model.Actor(request.form['gender'], request.form['height'], request.form['weight'], request.form['physique'])
    user = model.User(name, email, password, temp)
    db.session.add(temp)
    db.session.add(user)
    db.session.commit()
    return redirect(url_for('movies'))

Everything else works just fine, it is just that 'email' is not being saved as '[email protected]' and instead as NULL.

Thank you for your help ahead of time!

EDIT: Solved using sessions in Flask.

http://flask.pocoo.org/docs/quickstart/#sessions

like image 724
Sunny Malotrha Avatar asked Jul 31 '14 18:07

Sunny Malotrha


1 Answers

When you submit your signup form, you're using POST. Because you're using POST, your form values are added to request.form, not request.args.

Your email address will be in:

request.form.get('email')

If you were hitting the URL /[email protected], and you weren't rendering a template immediately with:

if request.method == 'GET':     
    return render_template('character.html') 

in your characters view, only then would you be able to access:

request.args.get('email')

Check out the werkzeug request / response docs for more info.

Edit: Here's a full working example (minus your models stuff)

app.py

 from flask import request, Flask, render_template, redirect, url_for       

app = Flask(__name__)                                                      
app.debug = True                                                           


@app.route('/signup', methods=['GET','POST'])                              
def signup():                                                              
    if request.method == 'GET':                                            
        return render_template('signup.html')                              
    email = request.form['email']                                          
    return redirect(url_for('character', email=email))                     


@app.route('/character', methods=['POST', 'GET'])                          
def character():                                                           
    email_from_form = request.form.get('email')                            
    email_from_args = request.args.get('email')                            
    return render_template('character.html',                               
                           email_from_form=email_from_form,                
                           email_from_args=email_from_args)                


if __name__ == '__main__':                                                 
    app.run() 

templates/signup.html

<html>                                                                     

Email from form: {{ email_from_form }} <br>                                
Email from args: {{ email_from_args }}                                     

</html>  

templates/character.html

<html>                                                                     

<form name="test" action="/character" method="post">                       
    <label>Email</label>                                                   
    <input type="text" name="email" value="[email protected]" />              
    <input type="submit" />                                                
</form>                                                                    

</html>      

Submitting the signin form (via POST) will populate Email from form

Hitting the url http://localhost:5000/[email protected] (via GET) will populate Email from args

like image 141
Chris McKinnel Avatar answered Oct 18 '22 03:10

Chris McKinnel