因为最近在学习python,做做笔记,方便以后的查阅。
Flask Web
Flask 是一个轻量级的 Web 应用框架, 使用 Python 编写。基于 WerkzeugWSGI工具箱和 Jinja2模板引擎。使用 BSD 授权。Flask也被称为 “microframework” ,因为它使用简单的核心,用 extension 增加其他功能。Flask没有默认使用的数据库、窗体验证工具。然而,Flask保留了扩增的弹性,可以用 Flask-extension 加入这些功能:ORM、窗体验证工具、文件上传、各种开放式身份验证技术。
实现简单的登录及验证功能(不使用数据库)
安装Flask
按照以下层级创建项目文件/夹
/application
/static ### 存放静态文件,比如CSS,JavaScript
/templates ### 存放HTML文件
/.py ###
应用包括主页-index(显示登录状态)、登录页面-login和错误页面-error,以及login.py来实现页面之间的联系,当然还有静态文件。为简单起见:
/static/style.css
body{color:green}
首先login.py:
#!/usr/bin/python
# -*- coding: utf8 -*-
from flask import Flask, request, render_template, url_for
from flask import abort, redirect, session, escape, make_response
import os
import sys
reload(sys)
sys.setdefaultencoding('utf8')
app = Flask(__name__)
app.secret_key = 'i like python'
### route装饰器用于把一个函数绑定到一个URL
@app.route('/')
def index():
if 'username' in session:
return render_template('index.html', message="hello %s" % escape(session['username']))
return render_template('index.html')
@app.route('/login', methods=['get', 'post'])
def login():
if request.method == "POST":
if request.form['username'] == "Alex" and request.form['password'] == "alex":
session['username'] = request.form['username']
session['password'] = request.form['password']
return redirect(url_for('index'))
else:
return render_template('error.html',error="账号或密码不正确!!!"),401
return render_template('index.html'),200
### 因为login页面是在index页面中引用的,所以不能通过修改URL为.../login进入登录页面,为用户友好,利用error.html来输出错误问题
else:
abort(403)
@app.errorhandler(403)
def page_not_403(error):
resp = make_response(render_template('error.html', error='你不能这样登录!!!'),403)
resp.headers['X-Somthing'] = 'You can not login in this way'
return resp
if __name__ =='__main__':
### 开启调试模式,如果运行过程中有错误,会在浏览器中显示
app.debug = True
app.run()
接着index.html
<!doctype html>
<html>
<head>
<title>index</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" type="text/css">
</head>
<body>
{% if message %}
<h1>>{{ message }}</h1>
{% else %}
<p><h1>请登录!</h1></p>
{% extends "login.html" %}
{% endif %}
</body>
</html>
再则login.html
<!doctype html>
<html>
<head>
<title>login</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" type="text/css">
</head>
<body>
<form action="/login" method="post">
<p>username: <input type="text" name="username"></p>
<p>password: <input type="password" name="password"></p>
<p><input type="submit" vaule="login"></p>
</form>
</body>
</html>
运行后在主页显示的就是index的内容加上login的内容
最后error.html
<!doctype html>
<html>
<head>
<title>error</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" type="text/css">
</head>
<body>
{% if error %}
<h1>{{ error }}!</h1>
{% else %}
<h1>您未获授权,无法查看此网页</h1>
{% endif %}
</body>
</html>
运行结果:
1 运行login.py,右击打开http://127.0.0.1:5000/,在主页显示如上图所示,因为初次打开,没有键入登录信息。
2 输入错误登录信息以及修改URL进入login页面
3 正常登录
ps: 在html文件中的引用外部样式表href=”{{ url_for(‘static’, filename = ‘style.css’) }}”可以在login.py文件中每次调用render_template()时添加参数style(自定义) = url_for(‘static’, filename = ‘style.css’),此时html文件中就可以改成href=”{{ style(render_template()中添加的参数) }}”