从html表单发布时,Flask/Python 3内部服务器错误500
我对Python&Flask还不熟悉,正在尝试设置一个非常基本的脚本,该脚本接收表单提交的信息并将其发布到新页面(我知道,非常简单,对吧?) 我的成功有限,不知道这里有什么问题。当我在python文件中选择了4个表单字段中的2个时,它就工作了:从html表单发布时,Flask/Python 3内部服务器错误500,python,html,forms,python-3.x,flask,Python,Html,Forms,Python 3.x,Flask,我对Python&Flask还不熟悉,正在尝试设置一个非常基本的脚本,该脚本接收表单提交的信息并将其发布到新页面(我知道,非常简单,对吧?) 我的成功有限,不知道这里有什么问题。当我在python文件中选择了4个表单字段中的2个时,它就工作了: name=request.form['name'] age=request.form['age'] 这很好,并且实现了我期望的功能—呈现包含“name”和“age”的output.html页面 但是,只要我尝试添加更多,我就会得到一个内部服务器错误(
name=request.form['name']
age=request.form['age']
这很好,并且实现了我期望的功能—呈现包含“name”和“age”的output.html页面
但是,只要我尝试添加更多,我就会得到一个内部服务器错误(500),即使我复制和粘贴了完全相同的代码,并且只更改了变量(例如,.py文件和输入输出html文件中的“number”和“feeling”)
这是密码
Python代码:
(输入表单位于/input/page上。“input_1”呈现output.html文件)
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>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>
(包含输入表单)
这可能是很明显的事情,但我就是看不见
谢谢你的帮助 当您必须生成url时,通常使用
url\u作为
。当我必须传递多个参数时,我宁愿不让事情复杂化。我要做的就是:
<form method="post" action="/input">
但是如果你真的想要生成url,那么就把你想要生成url的函数放进去,并传递参数
<form method="post" action={{url_for('input_1',name=name)}}>
您是否尝试过打开调试、异常、查看flask服务器的输出等功能?如果您将此功能减少为(发布到SO时通常应该执行的操作),它就可以正常工作。所以你的问题是别的。这对你的模板来说很好。如果不是恶意的话,应该有一个解释来支持向下投票。
@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)
<form method="post" action="/input">
@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)
<form method="post" action={{url_for('input_1',name=name)}}>
@app.route('/input/<name>') #you can add parameters as per your wish
def input_1(name):
...
...