request.args.get('key')给出NULL-Flask [英] request.args.get('key') gives NULL - Flask

查看:114
本文介绍了request.args.get('key')给出NULL-Flask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将变量"email"从我看来的"signup"方法传递给"character"方法.但是,

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

request.args.get('email') 

正在将NULL保存到数据库中.我不知道为什么.

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

将'email'变量传递给'/character'之后,显示的内容如下:

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

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

这是我在"views.py"中的代码:

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

其他一切都很好,只是电子邮件"没有保存为"test@test.com",而是保存为NULL.

Everything else works just fine, it is just that 'email' is not being saved as 'test@test.com' and instead as NULL.

谢谢您的帮助!

使用Flask中的会话解决了.

Solved using sessions in Flask.

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

推荐答案

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

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

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

If you were hitting the URL /characters?email=someemail@test.com, 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')

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

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

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

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

templates/signup.html

<html>                                                                     

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

</html>  

templates/character.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)提交登录表单将填充来自表单的电子邮件

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

点击网址 http://localhost:5000/character?email=test@email.com (通过GET)将填充来自args的电子邮件

Hitting the url http://localhost:5000/character?email=test@email.com (via GET) will populate Email from args

这篇关于request.args.get('key')给出NULL-Flask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆