创建app.py:
fromflaskimportFlask
fromflaskimportFlask,flash,redirect,render_template,request,session,abort
importos
app=Flask(__name__)
@app.route(‘/’)
defhome():
ifnotsession.get(‘logged_in’):
returnrender_template(‘login.html’)
else:
return”HelloBoss!”
@app.route(‘/login’,methods=[‘POST’])
defdo_admin_login():
ifrequest.form[‘password’]==’password’andrequest.form[‘username’]==’admin’:
session[‘logged_in’]=True
else:
flash(‘wrongpassword!’)
returnhome()
if__name__==”__main__”:
app.secret_key=os.urandom(12)
app.run(debug=True,host=’0.0.0.0′,port=4000)
创建模板文件./templates/login.html,主要代码如下:
<formaction=”/login”method=”POST”>
<inputtype=”username”name=”username”placeholder=”Username”>
<inputtype=”password”name=”password”placeholder=”Password”>
<inputtype=”submit”value=”Login”>
</form>
执行:
pythonapp.py
访问:
http://127.0.0.1:4000