-1
如果直接传入字符串,我可以访问request.form
上的值。但是,如果将字符串分配给变量并尝试使用该变量,则会失败。为什么下面的第二个打印声明不起作用?将变量传递给request.form.get失败,直接传递字符串
@app.route("/test", methods=['GET', 'POST'])
def test():
if request.method == 'POST':
# this works fine
print(request.form.get('lastname'))
# this doesn't -- why?
somevar = '\'lastname\''
print(request.form.get(somevar))
return "<p> check console </p>"
else:
return render_template('test.html')
<form method="post">
<input type="text" name="lastname">
<button type="submit" >Submit</button>
</form>
关键是'lastname'。你通过了'姓氏'。请注意,第二个字符串包含单引号,这不等同。 '“lastname”!=“'lastname'”' – davidism
谢谢,我以为关键是'姓'而不是姓。 – theciscodisco