写一个flask界面
时间: 2024-01-30 10:02:56 浏览: 22
好的,下面是一个简单的 Flask 界面示例:
```python
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/result', methods=['POST'])
def result():
name = request.form['name']
age = request.form['age']
return render_template('result.html', name=name, age=age)
if __name__ == '__main__':
app.run(debug=True)
```
该示例包含两个路由函数,一个用于渲染主页(`index()`),另一个用于处理表单提交并返回结果(`result()`)。在 `index.html` 模板中,我们创建了一个简单的表单,用户可以输入姓名和年龄信息。在 `result.html` 模板中,我们展示了用户提交的信息。
下面是 `index.html` 模板代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Flask Form Example</title>
</head>
<body>
<h1>Enter Your Information</h1>
<form method="POST" action="/result">
<label>Name:</label>
<input type="text" name="name" required>
<br><br>
<label>Age:</label>
<input type="text" name="age" required>
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
```
下面是 `result.html` 模板代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Flask Form Example</title>
</head>
<body>
<h1>Thank You!</h1>
<p>Your name is {{ name }} and you are {{ age }} years old.</p>
</body>
</html>
```
运行该代码后,用户可以在网页上输入姓名和年龄信息,提交表单后会显示一个包含用户信息的页面。