伸缩自如的Flask [day 7] Post data with Form tag

可以先把下面的范例载下来,并执行app.py:
https://github.com/wilsonsujames/flask_tutorial/tree/main/Flask_with_form

https://ithelp.ithome.com.tw/upload/images/20210513/20122678237IzfYqvb.png

可以在index.html的区块看见form这个区块含有两个input:

<form action="{{ url_for('login_process_fun') }}" method="POST">
  <div class="form-group">
  <label for="exampleInputEmail1">Email address</label>
  <input type="email" class="form-control" id="exampleInputEmail1" name='inputEmail' aria-describedby="emailHelp">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
        </div>
<div class="form-group">
  <label for="exampleInputPassword1">Password</label>
  <input type="password" class="form-control" id="exampleInputPassword1" name='inputpass'>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
  </form>

可以看见有form上面有action是对应了按了型态为submit的按钮之後,寻找login_process_fun这个function的名称。

在app.py中,我们也可以看见login_process_fun这个function对应了哪一个路由:

@app.route('/login_process',methods=[ "GET",'POST'])
def login_process_fun():

在使用form时,input我们会加上name的属性以方便後端的login_process_fun这个function可以获取input的值:

email=request.form.get('inputEmail')
psword=request.form.get('inputpass')
print(request.form)

我们也可以将字典形式的form印出来,并且分别获取名子为inputEmail及inputpass以便做後续处理或是呈现在网页上。
在login_process_fun最後回传了loginSuccess.html这个页面,并且带着email及psword呈现在网页上。

return render_template('loginSuccess.html',email=email,psword=psword)

一个礼拜了,不知道大家的股票怎麽样,反正我的联电是爆开了。
有的时候因为有着执着所以死撑着不放手,但是自己的目标初衷如果是为了跟别人拚枪对决的零和游戏,应该要玩的灵巧一点,该退就退。如果观点是着眼未来,我想当别人恐惧的时候,就是我贪婪的时候。


<<:  LEAP(轻量级可扩展认证协议)

>>:  iPhone维修售後网点如何选择?从了解维修基础知识开始

Flexbox-30天学会HTML+CSS,制作精美网站

以前排版都会使用float、display属性的block、inline来制作,现在有了Flexbo...

Day 17 免费个资隐私盘点工具

无论是企业组织都需要保留其用户个资处理活动的记录并涵盖资料处理目的、数据存留方式等,除了你可自行定义...

JavaScript - 让你的浏览器公威罗!

本篇要介绍的是让浏览器说话的功能,其实要让它说话很简单,浏览器本身就有内建方法可以使用,接下来让我们...

[ Day 1 ] - 变数与型别

变数与型别 变数是什麽呢? 把他想成是一个容器,容器可以存放需要的资讯,这一个资讯可以是一段文字、一...

[Day-26] math函式库(二)

今天要来延续上次所学的 「cmath」函式库 一开始一样先引入函式库 #include <cm...