【发布时间】:2018-02-05 22:37:04
【问题描述】:
我是 Python 和 Flask 的新手,正在尝试设置一个非常基本的脚本,该脚本从表单中获取提交的信息并将其发布到新页面(我知道,很简单吧?)
我的成功有限,无法弄清楚这里的问题所在。当我在 python 文件中选择了 4 个表单字段中的 2 个时,它正在工作:
name=request.form['name']
age=request.form['age']
这可以正常工作,并且符合我的预期 - 呈现包含“姓名”和“年龄”的 output.html 页面
但一旦我尝试添加更多内容,我就会收到内部服务器错误 (500),即使我正在复制和粘贴完全相同的代码并且只更改变量(即“数字”和“感觉') - 在 .py 文件和输入和输出 html 文件中。
这是代码..
Python 代码:
(输入表单在 /input/ 页面上。“input_1”呈现 output.html 文件)
from flask import Flask, render_template, request, url_for, redirect
from dbconnect import connection
from flask_debugtoolbar import DebugToolbarExtension
app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'kuywergiukrewgkbyuwe'
toolbar = DebugToolbarExtension(app)
app.config.update(TEMPLATES_AUTO_RELOAD = True)
@app.route('/')
def homepage():
return render_template ("main.html")
@app.route('/input/')
def input():
return render_template ("input.html")
@app.route('/input/', methods=["POST"])
def input_1():
name=request.form['name']
age=request.form['age']
number=request.form['number']
feeling=request.form['feeling']
return render_template('output.html', name = name, age = age, number = number, feeling = feeling)
if __name__ == "__main__":
app.run()
input.html 文件:
(包含输入表单)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>devserver</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="stylesheet" href="{{ url_for("static", filename="css/bootstrap.css") }}">
<link rel="shortcut icon" href="{{ url_for("static", filename="favicon.ico") }}">
</head>
<body>
<div class="container">
<div class="col">
<h2>Input form</h2>
<br>
<div class="form-group" >
<form method="post" action="{{ url_for('input') }}">
<label for="InputForm">Name</label>
<input type="text" name="name" class="form-control"/>
<label for="InputForm">Age</label>
<input type="text" name="age" class="form-control"/>
<label for="InputForm">Number</label>
<input type="text" name="number" class="form-control"/>
<label for="InputForm">Feeling</label>
<input type="text" name="feeling" class="form-control"/>
<br>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</body>
</html>
output.html 文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>devserver</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="stylesheet" href="{{ url_for("static", filename="css/bootstrap.css") }}">
<link rel="shortcut icon" href="{{ url_for("static", filename="favicon.ico") }}">
</head>
<body>
<div class="container">
<div class="col">
<h2>Output form</h2>
<br>
<div class="form-group" >
<form>
<h3>Output 1</h3>
<P>Your name is = {{name}}</p>
<h3>Output 2</h3>
<P>Your age is = {{age}} </p>
<h3>Output 3</h3>
<P>Your number is = {{number}}</p>
<h3>Output 4</h3>
<P>Your feeling is = {{feeling}} </p>
</form>
</div>
</div>
</div>
</body>
</html>
我不明白为什么它只适用于 2。当我注释掉以下内容时它工作正常:
@app.route('/input/', methods=["GET","POST"])
def input():
name=request.form['name']
age=request.form['age']
#number=request.form['number']
#feeling=request.form['feeling']
return render_template('output.html', name = name, age = age) #number = number, feeling = feeling)
这可能很明显,但我就是看不到。
感谢您的帮助!
【问题讨论】:
-
您是否尝试过打开调试、异常、查看烧瓶服务器的输出等?
-
如果你把它减少到minimal reproducible example(在发布到 SO 时通常应该做的事情),它工作得很好。所以你的问题是别的。这个gist.github.com/pvg/331760af664b34bda5a895d073334e81 与您的模板配合得很好。
标签: python html forms python-3.x flask