用Python打造一个简易的研究生管理系统App
2025-03-31 00:07
大家好,今天我们来聊聊如何用Python开发一个简单但功能齐全的研究生管理系统App。这个系统可以用来管理研究生的信息,比如他们的姓名、学号、专业等。听起来是不是很酷?
首先,我们需要确定我们的技术栈。我建议用Flask这个轻量级的Web框架,因为它上手快,而且适合小项目。对于数据库,SQLite是一个不错的选择,因为它不需要额外的配置,直接嵌入到应用里。
我们先创建一个简单的项目结构:
project/ ├── app.py ├── templates/ │ └── index.html ├── static/ └── database.db
在`app.py`里,我们需要导入必要的库并设置路由。首先安装Flask和SQLite,可以用pip安装:
pip install Flask pip install sqlite3
然后在`app.py`里写一些基本的代码:
from flask import Flask, render_template, request, redirect, url_for import sqlite3 app = Flask(__name__) def get_db_connection(): conn = sqlite3.connect('database.db') conn.row_factory = sqlite3.Row return conn @app.route('/') def index(): conn = get_db_connection() students = conn.execute('SELECT * FROM students').fetchall() conn.close() return render_template('index.html', students=students) if __name__ == '__main__': app.run(debug=True)
接下来,我们要创建数据库表。在`app.py`中添加以下代码来初始化数据库:
def init_db(): conn = get_db_connection() conn.execute(''' CREATE TABLE IF NOT EXISTS students ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, student_id TEXT UNIQUE NOT NULL, major TEXT NOT NULL ) ''') conn.commit() conn.close() if __name__ == '__main__': init_db() app.run(debug=True)
最后,我们需要一个HTML模板来展示学生信息。在`templates/index.html`里写上:
<!DOCTYPE html> <html> <head> <title>研究生管理系统</title> </head> <body> <h1>研究生管理系统</h1> <table border="1"> <tr><th>ID</th><th>姓名</th><th>学号</th><th>专业</th></tr> {% for student in students %} <tr> <td>{{ student['id'] }}</td> <td>{{ student['name'] }}</td> <td>{{ student['student_id'] }}</td> <td>{{ student['major'] }}</td> </tr> {% endfor %} </table> </body> </html>
这样,我们就完成了一个简单的研究生管理系统App。你可以通过浏览器访问它,看到学生的信息列表。
希望这篇教程对你有帮助!如果你有任何问题,欢迎随时问我。
本站知识库部分内容及素材来源于互联网,如有侵权,联系必删!
标签:Python