我正在按照教程创建HTML表单并获取用户输入。 但是在本教程中,一切正常。
首先,我因避免将GET放在app.route上而出错。在Tutorial上只需要设置POST。所以,我都设置了。而且有效。
但是现在,如果我尝试获取输入数据,则将获得方法不允许。 我该如何解决?
@app.route('/cars/novo', methods=['GET','POST',])
def addCarro():
''' HERE I GOT THE PROBLEM
marca = request.form['marca']
modelo = request.form['modelo']
placa = request.form['placa']
data_apreensao = request.form['data_apreensao']
motivo = request.form['motivo']
situacao = request.form['situacao']
apreensao = Apreensao(marca,modelo, placa, data_apreensao, motivo, situacao, apreensao)
'''
return render_template('apreensaoForm.html', titulo='Nova Apreensao')
html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../static/bootstrap/css/bootstrap.css">
<title>Formulário de Apreensao</title>
</head>
<body>
<div class="container">
<div class="page-header">
<h1>{{ titulo }}</h1>
</div>
<form action="/cars/novo" method="post">
<fieldset>
<div class="form-group">
<label for="marca">marca</label>
<input type="text" id="marca" name="marca" class="form-control">
</div>
<div class="form-group">
<label for="modelo">modelo</label>
<input type="text" id="modelo" name="modelo" class="form-control">
</div>
<div class="form-group">
<label for="placa">placa</label>
<input type="text" id="placa" name="placa" class="form-control">
</div>
<div class="form-group">
<label for="data-apreensao">data da apreensão</label>
<input type="date" id="data-apreensao" name="data-apreensao" class="form-control">
</div>
<div class="form-group">
<label for="motivo">motivo</label>
<input type="text" id="motivo" name="motivo" class="form-control">
</div>
<div class="form-group">
<label for="situacao">situação</label>
<input type="text" id="situacao" name="situacao" class="form-control">
</div>
<button type="submit" class="btn btn-primary btn-salvar">Salvar</button>
</fieldset>
</form>
</div>
</body>
</html>
答案 0 :(得分:0)
代码需要
if request.method == 'POST':
在尝试从表单中提取内容之前。
查看位于https://www.railstutorial.org/book/advanced_login#code-user_model_remember的代码段