request.args.get('key')给出NULL - Flask

时间:2014-07-31 18:01:48

标签: python flask

我试图将变量'email'从我视图中的'signup'方法传递给'character'方法。但是,

request.args.get('email') 

将NULL保存到数据库中。我无法弄清楚原因。

以下是将'email'变量传递给'/ character'后显示的内容:

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

以下是'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'))

其他一切工作正常,只是'电子邮件'没有保存为'test@test.com'而是保存为NULL。

提前感谢您的帮助!

编辑: 解决了使用Flask中的会话。

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

1 个答案:

答案 0 :(得分:21)

当您提交注册表单时,您正在使用POST。由于您正在使用POST,因此您的表单值会添加到request.form,而不是request.args

您的电子邮件地址位于:

request.form.get('email')

如果您访问了网址/characters?email=someemail@test.com,并且您没有立即使用以下方式呈现模板:

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

在您的角色视图中,只有这样您才能访问:

request.args.get('email')

查看werkzeug request / response文档以获取更多信息。

编辑:这是一个完整的工作示例(减去你的模特的东西)

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() 

模板/ signup.html

<html>                                                                     

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

</html>  

模板/ character.html

<html>                                                                     

<form name="test" action="/character" method="post">                       
    <label>Email</label>                                                   
    <input type="text" name="email" value="test@email.com" />              
    <input type="submit" />                                                
</form>                                                                    

</html>      

提交登录表单(通过POST)将填充Email from form

点击网址http://localhost:5000/character?email=test@email.com(通过GET)将填充Email from args