flask(四)初步实现用户登录
登录页渲染
项目文件中创建目录static,用于存放静态文件,我这里使用的是H-ui.admin_v3.1.3.1,需要的可以自己去下载,将解压后的文件夹中的lib、static、temp放入刚创建的static中。
templates中创建文件:admin/login.html
<!DOCTYPE HTML>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<!--[if lt IE 9]>
<script type="text/javascript" src="{{url_for('static',filename='lib/html5shiv.js')}}"></script>
<script type="text/javascript" src="{{url_for('static',filename='lib/respond.min.js')}}"></script>
<![endif]-->
<link rel="stylesheet" href="{{url_for('static',filename='static/h-ui/css/H-ui.min.css')}}">
<link rel="stylesheet" href="{{url_for('static',filename='static/h-ui.admin/css/H-ui.login.css')}}">
<link rel="stylesheet" href="{{url_for('static',filename='static/h-ui.admin/css/style.css')}}">
<link rel="stylesheet" href="{{url_for('static',filename='lib/Hui-iconfont/1.0.8/iconfont.css')}}">
<!--[if IE 6]>
<script type="text/javascript" src="{{url_for('static',filename='lib/DD_belatedPNG_0.0.8a-min.js')}}"></script>
<script>DD_belatedPNG.fix('*');</script>
<![endif]-->
<title>登录 </title>
<meta name="keywords" content="后台登陆">
<meta name="description" content="后台登陆">
</head>
<body>
<input type="hidden" id="TenantId" name="TenantId" value="" />
<div class="header"></div>
<div class="loginWraper">
<div id="loginform" class="loginBox">
<form class="form form-horizontal" action="" method="post">
<div class="row cl">
<label class="form-label col-xs-3"><i class="Hui-iconfont"></i></label>
<div class="formControls col-xs-8">
<input id="username" name="username" type="text" placeholder="账户" class="input-text size-L">
</div>
</div>
<div class="row cl">
<label class="form-label col-xs-3"><i class="Hui-iconfont"></i></label>
<div class="formControls col-xs-8">
<input id="password" name="password" type="password" placeholder="密码" class="input-text size-L">
</div>
</div>
<div class="row cl">
<div class="formControls col-xs-8 col-xs-offset-3">
<label for="online">
<input type="checkbox" name="online" id="online" value="">
使我保持登录状态</label>
</div>
</div>
<div class="row cl">
<div class="formControls col-xs-8 col-xs-offset-3">
<input name="" type="submit" class="btn btn-success radius size-L" value=" 登 录 ">
<input name="" type="reset" class="btn btn-default radius size-L" value=" 取 消 ">
{% if message %}
<p style="color:red">{{ message }} </p>
{% endif %}
</div>
</div>
</form>
</div>
</div>
<div class="footer"></div>
<script type="text/javascript" src="{{url_for('static',filename='lib/jquery/1.9.1/jquery.min.js')}}"></script>
<script type="text/javascript" src="{{url_for('static',filename='static/h-ui/js/H-ui.min.js')}}"></script>
</body>
</html>
视图函数渲染静态页面:修改admin下的views.py
from flask import Blueprint, render_template
bp = Blueprint('admin',__name__)
@bp.route("/admin")
def index():
return render_template('admin/index.html')
访问:http://127.0.0.1:5000/admin/ 可查看运行效果
初步实现用户登录
修改admin/views.py文件:
from flask import Blueprint, render_template,request,session,redirect,url_for
from .models import Users
bp = Blueprint('admin',__name__,url_prefix='/admin')
@bp.route("/login/",methods=['GET','POST'])
def login():
error = None
if request.method == 'GET':
return render_template('admin/login.html')
else:
user = request.form.get('username')
pwd = request.form.get('password')
users = Users.query.filter_by(username=user).first()
if users:
if user == users.username and users.chek_password(pwd):
session['user_id'] = users.uid # 用户id存于session
return redirect(url_for('admin.index'))
else:
error = "用户名或密码错误"
return render_template('admin/login.html',message=error)
else:
return render_template('admin/login.html',message="别试了,没有此用户!")
@bp.route('/')
def index():
return render_template('admin/index.html')