一尘不染

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

flask

我试图将变量“ email”从我看来的“注册”方法传递给“字符”方法。然而,

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。


阅读 1267

收藏
2020-04-08

共1个答案

一尘不染

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

你的电子邮件地址将位于:

request.form.get('email')

如果你访问的是URL /characters?email=someemail@test.com,并且不是立即使用以下方法呈现模板:

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

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

request.args.get('email')

查看werkzeug请求/响应文档以获取更多信息。

编辑:这是一个完整的工作示例(减去模型的内容)

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="test@email.com" />              
    <input type="submit" />                                                
</form>                                                                    

</html>

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

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

2020-04-08