最近有些需求、想法,毕竟devops的风还是很强烈的啊,所以就跟风学学dev方面的东西; flask是一个很小巧很方便的webframe,听朋友说起django非常的重,于是我就在还有点python基础的能力下选择flask学学;准备用这个框架开发新的平台,首先就要有用户登录页面,用flask可以这样实现: 代码结构:
flasky
├── run.py
├── static
│ ├── av1.jpg
│ ├── av2.jpg
│ ├── av3.jpg
│ ├── bootstrap.min.css
│ ├── bootstrap.min.js
│ ├── bootstrap-responsive.min.css
│ ├── excanvas.min.js
│ ├── fullcalendar.css
│ ├── fullcalendar.min.js
│ ├── glyphicons-halflings.png
│ ├── GNU-Linux-Logo-Penguin-SVG.png
│ ├── jquery.flot.min.js
│ ├── jquery.flot.resize.min.js
│ ├── jquery.min.js
│ ├── jquery.peity.min.js
│ ├── jquery.ui.custom.js
│ ├── logo.png
│ ├── Tux.jpg
│ ├── unicorn.dashboard.js
│ ├── unicorn.grey.css
│ ├── unicorn.js
│ ├── unicorn.login.css
│ ├── unicorn.login.js
│ └── unicorn.main.css
└── templates
├── base.html
├── check.html
├── index.html
├── login.html
└── upload.html
前端就用bootstrap展示,login.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| {% extends \"base.html\" %} {% block title %} Dachen FTP {% endblock %} </style></head> {% block body %} <body>
<div id="logo"> <img src="{ {url_for('static',filename='GNU-Linux-Logo-Penguin-SVG.png')}}" alt=""> </div> <div id="loginbox"> <form id="loginform" class="form-vertical" action="" method="post"> <p>Sing in to blog.sctux.com</p> <div class="form-group"> <div class="controls"> <div class="input-prepend"> <input name="username" value="{ {request.form.username}}" placeholder="Username"> </div> </div> </div> <!\-\- <div class="control-group"> --> <div class="form-group"> <div class="controls"> <div class="input-prepend"> <input name="pass" placeholder="Password "> </div> </div> </div> <div class="form-group"> <div class="col-md-90 col-md-offset-12"> <button type="submit" class="btn btn-primary">Sign in</button> </div> {% if error %} <p><font color="red">{ { error }}</font></p> {% endif %} </div> </form> {% endblock %} </body>
|
run.py内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| \ from flask.ext.wtf import Form from flask import render\_template, redirect, url\_for
app = Flask(\_\_name\_\_)
@app.route('/login',methods=\['POST','GET'\]) def login(): error=False if request.method == 'POST': if request.form\['username'\] != 'user1' or request.form\['pass'\] != 'user1@1qaz': error="username or password error !" else: return redirect(url_for('index')) return render_template('login.html',error=error)
@app.route('/index') def index(): return render_template('index.html',result=result)
@app.route('/upload',methods=\['GET','POST'\]) def upload(): return render_template('upload.html')
@app.route('/check',methods=\['POST','GET'\]) def check(): return render_template('check.html')
@app.route('/logout') def logout(): return redirect(url_for('login'))
if \_\_name\_\_=="\_\_main\_\_": app.run(debug=True,host='xxxxxxxxx',port=4000)
|
启动 python run.py 访问 http://xxxxxxxxx:4000/
登录主页的效果:
好啦,一个简单的登录页面就写好啦;flask 值得你拥有!