基于学工管理系统的呼和浩特高校学生登录模块设计与实现
2025-06-07 13:18
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Student(db.Model):
id = db.Column(db.Integer, primary_key=True)
student_id = db.Column(db.String(20), unique=True, nullable=False)
name = db.Column(db.String(50), nullable=False)
password = db.Column(db.String(100), nullable=False)

接下来是登录接口的代码:
from flask import Flask, request, jsonify
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///students.db'
db.init_app(app)
@app.route('/login', methods=['POST'])
def login():
data = request.get_json()
student_id = data.get('student_id')
password = data.get('password')
student = Student.query.filter_by(student_id=student_id).first()
if student and student.password == password:
return jsonify({"message": "Login successful!", "status": "success"})
else:
return jsonify({"message": "Invalid credentials", "status": "failure"})
if __name__ == '__main__':
with app.app_context():
db.create_all()
app.run(debug=True)
from flask_bcrypt import Bcrypt
bcrypt = Bcrypt(app)
# 修改后的Student类
class Student(db.Model):
id = db.Column(db.Integer, primary_key=True)
student_id = db.Column(db.String(20), unique=True, nullable=False)
name = db.Column(db.String(50), nullable=False)
password_hash = db.Column(db.String(100), nullable=False)
# 修改后的注册函数
@app.route('/register', methods=['POST'])
def register():
data = request.get_json()
student_id = data.get('student_id')
name = data.get('name')
password = data.get('password')
hashed_password = bcrypt.generate_password_hash(password).decode('utf-8')
new_student = Student(student_id=student_id, name=name, password_hash=hashed_password)
db.session.add(new_student)
db.session.commit()
return jsonify({"message": "User registered successfully!"})

本站知识库部分内容及素材来源于互联网,如有侵权,联系必删!
标签:学工管理系统

