可以先把下面的範例载下来,并执行app.py:
https://github.com/wilsonsujames/flask_tutorial/tree/main/Flask_with_form
可以在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)
一个礼拜了,不知道大家的股票怎么样,反正我的联电是爆开了。
有的时候因为有着执着所以死撑着不放手,但是自己的目标初衷如果是为了跟别人拚枪对决的零和游戏,应该要玩的灵巧一点,该退就退。如果观点是着眼未来,我想当别人恐惧的时候,就是我贪婪的时候。