我是Python&的新手。 Flask并尝试设置一个非常基本的脚本,从表单提交信息并将其发布到新页面(我知道,非常简单吧?)
我的成功有限,无法弄清问题在这里。当我在python文件中选择了4个表单字段中的2个时,它正在工作:
name=request.form['name']
age=request.form['age']
这样可以正常工作并完成我期望的操作 - 呈现包含' name'的output.html页面。 &安培; '年龄'
但是一旦我尝试添加,我就会收到内部服务器错误(500),即使通过我复制& amp;粘贴完全相同的代码并仅更改变量(即'数字''感觉') - 在.py文件和输入&输出html文件。
继承代码..
Python代码:
(输入表单位于/ input / page。" 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)
这可能是非常明显的事情,但我无法看到它。
感谢您的帮助!
答案 0 :(得分:-1)
当您必须生成网址时,通常会使用url_for
。当我必须传递多个参数时,我宁愿不要使问题复杂化。我会做的就是这样:
<form method="post" action="/input">
并在app文件中:
@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)
但是如果你真的想生成url,那么就把你想要生成url的函数放进去并传递参数。
<form method="post" action={{url_for('input_1',name=name)}}>
然后像这样调用功能input_1
:
@app.route('/input/<name>') #you can add parameters as per your wish
def input_1(name):
...
...