zoukankan      html  css  js  c++  java
  • 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>

     

    
    
  • 相关阅读:
    利用for循环 修改精灵图背景位置
    添加列表项 避免浏览器反复渲染 Fragment
    向元素添加属性名和属性值
    分割文本节点
    查询、回显 基本功能
    获取注释
    合并文本节点
    Node(节点)的4个操作方法
    setTimeout与setInterval
    javascript循环
  • 原文地址:https://www.cnblogs.com/wanghaonull/p/6343663.html
Copyright © 2011-2022 走看看