主页 > 编程 > python >

学生信息管理系统python毕业设计 管理员登录模块

2019-01-15 16:07 阅读:85 来源:智宇SEO自媒体

  运行环境:win10 64位 python3.5   Flask 0.12  pymysql 0.7.11   mysql 5.5

  学生信息管理系统python毕业设计 管理员登录模块

  实现:管理员登录,记录登录时间和登录IP。

学生信息管理系统python毕业设计 管理员登录模块

  控制器模块代码:

from flask import Flask,render_template,request,url_for,redirect,session,flash
import time
from datetime import timedelta
from db import *
import os
import config

app = Flask(__name__)
#静态文件缓存时间设置
app.config['SEND_FILE_MAX_AGE_DEFAULT']=timedelta(seconds=1)
app.config.from_object(config)
app.config['SECRET_KEY']=os.urandom(24)

@app.route('/login',methods=['GET','POST'])
def login():
    if request.method == 'POST':
        uname = request.form['uname']
        pwd   = request.form['pwd']
        #登录验证
        data=loginCheck(uname,pwd)
        if data:
            session['username'] = uname
            #登录时间
            loginTime =time.time()
            #登录ip
            loginIp=request.remote_addr
            #管理员id
            id=data[0][0]
            setAdminLoginTimeIp(loginTime,loginIp,id)
            return redirect(url_for('index'))
        else:
            flash("用户名或者密码错误")
            return render_template('login.html')
    return render_template('login.html')

@app.route('/index')
def index():
    #判断是否为已登录状态
    if (session.get('username') == None):
        return redirect(url_for('login'))
    else:
        uname = session.get('username')
        return  render_template('index.html',uname=uname)
#退出系统
@app.route('/out')
def out():
    #注销session
    session.pop('username')
    return redirect(url_for('login'))
if __name__ == '__main__':
    app.run()

  模型模块代码:

import pymysql

# 管理员登录
def mysqlConn():
    conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="root", db="stuby", charset="utf8")
    return conn;
def loginCheck(uname,pwd):
    conn=mysqlConn()
    cur = conn.cursor()
    sql = "select * from stu_admin where uname='%s' and pwd='%s'" % (uname, pwd)
    try:
        cur.execute(sql)
        result = cur.fetchall()
        if (len(result)) == 0:
            return False
        else:
            return result
    except Exception as e:
        raise e
    finally:
        conn.close()

#记录管理员登录时间和IP
def setAdminLoginTimeIp(loginTime,loginIp,id):
    conn = mysqlConn()
    cur = conn.cursor()
    sql="update stu_admin set logintime= '%d', loginip= '%s' where id = '%d'"  %(loginTime,loginIp,id)

    try:
        cur.execute(sql)
        # 提交
        conn.commit()
    except Exception as e:
        # 错误回滚
        conn.rollback()
    finally:
        conn.close()

  视图模块代码:
 

<html>
<head lang="en">
  <meta charset="UTF-8">
  <title>Login Page | Amaze UI Example</title>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="format-detection" content="telephone=no">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <link rel="stylesheet" href="{{url_for('static',filename='css/amazeui.min.css')}}"/>
  <style>
    .header {
      text-align: center;
    }
    .header h1 {
      font-size: 200%;
      color: #333;
      margin-top: 30px;
    }
    .header p {
      font-size: 14px;
    }
  </style>
</head>
<body>
<div class="header">
  <div class="am-g">
    <h1>学生信息管理系统</h1>
    <p>Student Information Management System<br/>

    </p>
  </div>
  <hr />
</div>
<div class="am-g">
  <div class="am-u-lg-6 am-u-md-8 am-u-sm-centered">
    <h3>登录</h3>
    <hr>
   <tr>{{ get_flashed_messages()[0] }}</tr>

    <form method="post" class="am-form">
      <label for="text">用户名:</label>
      <input type="text" name="uname"  value="" ">
      <br>
      <label for="password">密码:</label>
      <input type="password" name="pwd" id="password" value="">
      <br>
      <label for="remember-me">
        <input id="remember-me" type="checkbox">
        记住密码
      </label>
      <br />
      <div class="am-cf">
        <input type="submit" name="" value="登 录" class="am-btn am-btn-primary am-btn-sm am-fl">
      
      </div>
    </form>
    <hr>
    <p>© 2019 </p>
  </div>
</div>
</body>
</html>