flask 简易注册登陆

db.py    

  

import MySQLdb
conn = MySQLdb.connect('localhost', 'root', '123456', 'test1')
cur = conn.cursor()

def addUser (username,password):
    sql="insert into user (username,password) VALUES ('%s','%s')"%(username,password)
    cur.execute(sql)
    conn.commit()
    conn.close()

def isExisted(username,password):
    sql = "select * from user where username='%s' and password = '%s' " %(username,password)
    cur.execute(sql)
    result = cur.fetchall()
    print result
    if len(result)==0:
        return False
    else:
        return True

main.py

  

from flask import Flask,url_for
from flask import request
from flask import render_template
from flask import redirect
from db import *
app = Flask(__name__)


@app.route('/login',methods=['GET','POST'])
def login():
    if request.method =='POST':
        username=request.form.get('username')
        password=request.form.get('password')
        if isExisted(username,password):
            message = "login success"
            return render_template('index.html',message=message)
        else:
            message = "login filed"
            return render_template('index.html',message=message)
    return render_template('index.html',message="weidenglu")

@app.route('/register', methods=['POST','GET'])
def register():
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        print (username)
        addUser(username,password)
        return "123"
    return render_template('index.html')
if __name__ == "__main__":
    app.debug = True
    app.run(port=7777)

html 

 

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h1>{{ message }} index.html2</h1>
    <h1>login</h1>
    <form action="/login" method="POST">
        <input type="text" name="username">
        <input type="password" name="password">
        <input type="submit" value="提交">
    </form>
</body>
</html>

 

 


posted @ 2017-01-23 11:37  webbky  阅读(549)  评论(0编辑  收藏  举报