Flask web项目(7): 将表单数据发送到模板

本文介绍了如何在Flask web项目中处理表单数据,并将其发送到模板进行显示。通过示例代码展示了后端如何接收前端表单提交的信息,以及解决在开发过程中遇到的'Address already in use'错误的方法。同时,提供了系列教程的相关链接,帮助读者深入理解Flask的使用。
摘要由CSDN通过智能技术生成

实现提交表单功能
在这里插入图片描述
在这里插入图片描述
student.html

<html>
   <body>
   
      <form action = "http://0.0.0.0:5000/result" method = "POST">
         <p>Name <input type = "text" name = "Name" /></p>
         <p>Physics <input type = "text" name = "Physics" /></p>
         <p>Chemistry <input type = "text" name = "chemistry" /></p>
         <p>Maths <input type ="text" name = "Mathematics" /></p>
         <p><input type = "submit" value = "submit" /></p>
      </form>
      
   </body>
</html>

result.html

<!doctype html>
<html>
   <body>

      <table border = 1>
         {% for key, value in result.items() %}

            <tr>
               <th> {{ key }} </th>
               <td> {{ value }} </td>
            </tr>

         {% endfor %}
      </table>

   </body>
</html>

后端代码:

from flask import Flask, render_template, request
app = Flask(__name__)

@app.route('/')
def student():
   return render_template('student.html')

@app.route('/result',methods = ['POST', 'GET'])
def result():
   if request.method == 'POST':
      result = request.form
      return render_template("result.html",result = result)



if __name__ == '__main__':
   app.run(
      host='0.0.0.0',
      port= 5000,
      debug=True
   )

参考链接: https://blog.csdn.net/weixin_41287692/article/details/88951211

遇到的问题:
报错: Address already in use
解决方案:
在这里插入图片描述
参考链接:
https://blog.csdn.net/weixin_44141532/article/details/87937541

相关推荐

  1. Flask web项目(1): 创建虚拟环境+安装flask
  2. Flask web项目(2): 使用Pycharm创建第一个项目–hello world
  3. Flask web项目(3): pipenv 使用基本命令
  4. Flask web项目(4): route()装饰器
  5. Flask web项目(5): http协议
  6. Flask web项目(6): 使用flask模板
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值