一个简单的注册登录

from pymongo import MongoClient

MC = MongoClient('127.0.0.1', 27017)
MongoDB = MC['s2'] #创建数据库(内存中)
from flask import Flask, request, render_template, jsonify
from s2 import MongoDB

app = Flask(__name__)

@app.route('/reg', methods=['POST', 'GET'])
def reg():
if request.method == 'GET':
return render_template('reg.html')
else:
user_info = request.form.to_dict()
res = MongoDB.user.insert_one(user_info) # 创建表并插入一条数据
if res.inserted_id:
print('注册成功')
return '注册成功'
return '注册失败'

@app.route('/login', methods=['POST', 'GET'])
def login():
if request.method == 'GET':
return render_template('login.html')
else:
user_info = request.form.to_dict()
user_info_dict = MongoDB.user.find_one(user_info) #查询不到返回None
#user_info_dict['_id'] = str(user_info_dict['_id'])
if user_info_dict:
#return jsonify(user_info_dict)
return '登录成功'


if __name__ == '__main__':
app.run()


-------------------------------------------

个性签名:代码过万,键盘敲烂!!!

如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!