python 代码
from flask import Flask, request, render_template, redirect, url_forapp = Flask(__name__)@app.route('/')
def form():# 渲染表单页面return render_template('./index.html')@app.route('/submit_form', methods=['POST'])
def submit_form():# 从POST请求中获取数据name = request.form['name']email = request.form['email']# 在这里,你可以添加代码来处理这些数据,比如保存到数据库# 但为了演示,我们只是简单地将它们打印到控制台print(f"Name: {name}, Email: {email}")# 表单提交后重定向到某个页面,这里只是重定向回首页return redirect(url_for('form'))if __name__ == '__main__':app.run(debug=True)
HTML代码
<!DOCTYPE html>
<html>
<head> <title>简单表单</title>
</head>
<body> <h2>提交你的信息</h2> <form action="/submit_form" method="post"> <label for="name">姓名:</label><br> <input type="text" id="name" name="name"><br> <label for="email">邮箱:</label><br> <input type="email" id="email" name="email"><br><br> <input type="submit" value="提交">
</form> </body>
</html>
项目结构

结果

