我试图将变量'email'从我视图中的'signup'方法传递给'character'方法.然而,
request.args.get('email')
Run Code Online (Sandbox Code Playgroud)
将NULL保存到数据库中.我无法弄清楚为什么.
以下是将'email'变量传递给'/ character'后显示的内容:
http://127.0.0.1:5000/character?email=test%40test.com
Run Code Online (Sandbox Code Playgroud)
这是我在'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'))
Run Code Online (Sandbox Code Playgroud)
其他所有工作都很好,只是'电子邮件'没有保存为'test@test.com'而是保存为NULL.
提前感谢您的帮助!
编辑:使用Flask中的会话解决.
Chr*_*nel 23
当您提交注册表单时,您正在使用POST.因为您正在使用POST request.form,所以不会添加表单值request.args.
您的电子邮件地址将位于:
request.form.get('email')
Run Code Online (Sandbox Code Playgroud)
如果您正在访问该网址/characters?email=someemail@test.com,并且您没有立即使用以下方式呈现模板:
if request.method == 'GET':
return render_template('character.html')
Run Code Online (Sandbox Code Playgroud)
在您的角色视图中,只有这样您才能访问:
request.args.get('email')
Run Code Online (Sandbox Code Playgroud)
查看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()
Run Code Online (Sandbox Code Playgroud)
模板/ signup.html
<html>
Email from form: {{ email_from_form }} <br>
Email from args: {{ email_from_args }}
</html>
Run Code Online (Sandbox Code Playgroud)
模板/ 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>
Run Code Online (Sandbox Code Playgroud)
提交登录表单(通过POST)将填充 Email from form
点击网址http://localhost:5000/character?email=test@email.com(通过GET)将填充Email from args
| 归档时间: |
|
| 查看次数: |
28415 次 |
| 最近记录: |