flask 简易注册登陆
2024-08-27 22:31:10
db.py
import MySQLdb
conn = MySQLdb.connect('localhost', 'root', '', '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 ""
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>
最新文章
- Vector Tile
- (2)RGB-D SLAM系列- 工具篇(依赖库及编译)
- [原创]java WEB学习笔记66:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) 使用 paramsPrepareParamsStack 重构代码 ,PrepareInterceptor拦截器,paramsPrepareParamsStack 拦截器栈
- PHP多线程类
- jsp %EF%BB%BF
- iOS学习笔记之Category
- nyoj 119 士兵杀敌(三)(RMQ)
- 李洪强漫谈iOS开发[C语言-018]-scanf函数
- 由于java.util.Arrays.asList(...)导致的异常
- 4. sudo,PATH环境变量,修改字符集,ntpserver,加大文件描述符,隐藏内核版本,锁定关键系统文件
- 2013-9百度技术沙龙:Clouda与nodejs
- android studio 学习之一 安装和基本使用
- python自动化运维之路~DAY2
- Ubuntu 编译安装 nDPI
- iOS-UICollectionViewLayout方法介绍
- centos6编译安装zabbix3.0和中文支持整理文档
- Python入门1(简介、安装)
- 重命名IDEA14项目名
- Tomcat的HTTPS配置及HTTP自动跳转配置
- python教程(一)&#183;简介