既然需要用户认证,那么必须要存储用户信息。可以使用任何形式的存放方式。我用数据库,使用mysql。
一) 首先创建表:
1) .用户名 : usernm (用户不能重名,所以和id字段为联合主键)
2) .用户密码: passwd
3) .用户的id: uid (做为外键,可以作为其他表的主键); 一组数字。类似fans?id=66310254 或者这种
4) .用户邮箱:用于邮箱认证。 (暂时不设置该字段,以后再说)
create table users(
id int auto_increment,
usernm varchar(15),
passwd varchar(15),
uid char(8),
primary key (id)
)ENGINE=innodb auto_increment=1 default charset=utf8
登陆页面 index.html
参考zabbix的登陆页面,但是也只是很像,因为不会css和html
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta name="Author" content="Zabbix SIA">
<title>hexiantitle>
<link rel="icon" href="../static/images/saber.ico" type="image/x-icon"/>
<style type="text/css">
html {
position: relative;
min-height: 100%;
}
body {
font-family: Arial, Tahoma, Verdana, sans-serif;
font-size: 100%;
line-height: 1.4em;/*设置行高*/
color: #256384; /*文字的颜色*/
background-color: #ebeef0;
margin-bottom: 60px;/*设置下边距*/
min-width: 950px;
margin: 0;
padding: 0;
border: 0;
vertical-align: baseline;/*设置元素的垂直对齐方式*/
}
.msg-bad-global {
display: none;
position: fixed;
bottom: 0;
z-index: 10000;
width: 100%;
padding: 5px 5px;
text-align: center;
color: #593c00;
background-color: #ffc859;
border-top: 1px solid #ffc040;
}
.article{
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
vertical-align: baseline;
}
.server-name {
color: #768d99;
float: left;
margin-top:0px;
white-space: nowrap; /*规定段落中的文本不进行换行*/
overflow: hidden;/*overflow 属性规定当内容溢出元素框时发生的事情。我试了下默认就是超过后,隐藏*/
text-overflow: ellipsis; /*text-overflow 属性规定当文本溢出包含元素时发生的事情。显示省略符号来代表被修剪的文本*/
vertical-align: baseline;/*设置元素的垂直对齐方式*/
}
.signin-container {
background-color: #fff;
width: 480px;
margin: 0 auto;
margin-top: 5%;
padding: 42px 42px 39px 42px;
border: 1px solid #dfe4e7;
}
.signin-logo {
margin: 0 auto;
margin-bottom: 21px;
width: 419px;
height: 164px;
background: url(../static/images/logo.png) no-repeat }
form{
display: block; /*block 此元素将显示为块级元素,此元素前后会带有换行*/
}
label
{
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
display: inline-block;
margin: 0 0 2px 0 /*上右下左,顺势针*/ }
input:-webkit-autofill
{
background-color: rgb(250, 255, 189);
background-image: none;
color: rgb(0, 0, 0);
}
input[type="text"], input[type="password"]
{
min-height: 24px;
border: 1px solid #acbbc2;
transition: border-color 0.2s ease-out;
outline: 0;
box-sizing: border-box;
padding: 9px 5px;
width: 100%;
}
input{
font-family: Arial, Tahoma, Verdana, sans-serif;
font-size: 1em;
-webkit-writing-mode: horizontal-tb;
letter-spacing: normal;
word-spacing: normal;
text-transform: none;
text-indent: 0px;
text-shadow: none;
display: inline-block;
text-align: start;
text-rendering: auto;
-webkit-rtl-ordering: logical;
}
li{
vertical-align: baseline;
margin: 0;
border: 0px;
display: list-item;
list-style-type:none }
style>
head>
<body style>
<div class="msg-bad-global" id="msg-bad-global">div>
<div class="article"><div class="server-name">div>
<div class="signin-container"><div class="signin-logo">div>
<form method="post" action="{{ url_for('userLogin') }}" enctype=multipart/form-data accept-charset="utf-8">
<ul>
<li><label for="name">用户名label>
<input type="text" id="name" name="name" value="" maxlength="255" autofocus="autofocus">
li>
<li><label for="password">密码label>
<input type="password" id="password" name="password" value="" maxlength="255">
{% if get_flashed_messages() %}{% for x in get_flashed_messages() %}<div>{{x}}div>{% endfor %}{% endif %}
li>
<li><label for="autologin">
<input type="checkbox" id="autologin" name="autologin" value="1" checked="checked">Remember me for 30 dayslabel>
li>
<li><button type="submit" id="enter" name="enter" value="Sign in">登录button>li>ul>form>div>
body>html>
login.py
#encoding:utf-8
from flask import Flask,render_template,request,flash,url_for
app = Flask(__name__)
app.secret_key = "he234zse"
@app.route("/")
def hello(name=None):
return render_template('index.html')
@app.route("/userLogin",methods=['POST'])
def userLogin():
form = request.form
username = form.get("name")
password = form.get("password")
if not username:
flash(u"%s"%username)
return render_template("index.html")
if not password:
flash(u"密码为空")
return render_template("index.html")
if username=='123' and password=='123':
flash("ok!")
return render_template("index.html")
else:
flash(u"账号或者密码错误!")
return render_template("index.html")
if __name__ == '__main__':
app.run(host='0.0.0.0',debug=True)