Python request.args.get('key') 给出 NULL - Flask
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25065900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
request.args.get('key') gives NULL - Flask
提问by Sunny Malotrha
I am trying to pass the variable 'email' from the 'signup' method in my view to the 'character' method. However,
我试图将变量“email”从我认为的“signup”方法传递给“character”方法。然而,
request.args.get('email')
is saving NULL into the database. I cannot figure out why.
正在将 NULL 保存到数据库中。我不明白为什么。
Here is what shows up after passing the 'email' variable to '/character':
以下是将“email”变量传递给“/character”后显示的内容:
http://127.0.0.1:5000/character?email=test%40test.com
Here is my code in 'views.py':
这是我在“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.
其他一切都很好,只是“电子邮件”没有保存为“[email protected]”,而是保存为 NULL。
Thank you for your help ahead of time!
提前感谢您的帮助!
EDIT: Solved using sessions in Flask.
编辑:在 Flask 中使用会话解决。
回答by Chris McKinnel
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.
当您提交注册表单时,您正在使用 POST。因为您使用的是 POST,所以您的表单值被添加到request.form,而不是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:
如果您点击了 URL /[email protected],并且您没有立即使用以下内容呈现模板:
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 / responsedocs for more info.
查看werkzeug 请求/响应文档以获取更多信息。
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
模板/signup.html
<html>
Email from form: {{ email_from_form }} <br>
Email from args: {{ email_from_args }}
</html>
templates/character.html
模板/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
提交登录表单(通过 POST)将填充 Email from form
Hitting the url http://localhost:5000/[email protected](via GET) will populate Email from args
点击 url http://localhost:5000/[email protected](通过 GET)将填充Email from args

