2015-10-18 142 views
-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> 
+0

关键是'lastname'。你通过了'姓氏'。请注意,第二个字符串包含单引号,这不等同。 '“lastname”!=“'lastname'”' – davidism

+0

谢谢,我以为关键是'姓'而不是姓。 – theciscodisco

回答

1

我不明白,为什么你认为你需要加引号的。您没有:

somevar = 'lastname' 
print(request.form.get(somevar))