Python打造高效在线考试系统

在这个数字化时代,在线教育已成为主流,而在线考试系统则是其重要组成部分。本文将带你一步步用Python语言构建一个功能完备的在线考试系统,助你轻松应对各种考试需求。

Python打造高效在线考试系统

项目背景与需求分析

在线考试系统需满足以下基本需求:

  1. 用户注册与登录
  2. 题库管理
  3. 考试安排与进行
  4. 成绩统计与展示

技术选型

  • 后端:Python + Flask框架
  • 数据库:SQLite
  • 前端:HTML + CSS + JavaScript

系统架构设计

1. 用户模块

注册与登录

使用Flask的表单处理功能,结合SQLite数据库存储用户信息。

from flask import Flask, request, redirect, url_for, session
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///exam.db'
db = SQLAlchemy(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    password = db.Column(db.String(80), nullable=False)

@app.route('/register', methods=['POST'])
def register():
    username = request.form['username']
    password = request.form['password']
    new_user = User(username=username, password=password)
    db.session.add(new_user)
    db.session.commit()
    return redirect(url_for('login'))

@app.route('/login', methods=['POST'])
def login():
    username = request.form['username']
    password = request.form['password']
    user = User.query.filter_by(username=username, password=password).first()
    if user:
        session['user_id'] = user.id
        return redirect(url_for('dashboard'))
    return 'Invalid credentials'

2. 题库管理

题目添加与查询

通过后台管理界面添加题目,并支持按类别查询。

class Question(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.Text, nullable=False)
    category = db.Column(db.String(50), nullable=False)

@app.route('/add_question', methods=['POST'])
def add_question():
    content = request.form['content']
    category = request.form['category']
    new_question = Question(content=content, category=category)
    db.session.add(new_question)
    db.session.commit()
    return 'Question added successfully'

@app.route('/questions/<category>')
def get_questions(category):
    questions = Question.query.filter_by(category=category).all()
    return {'questions': [q.content for q in questions]}

3. 考试模块

考试安排与进行

设置考试时间,考生在规定时间内作答。

@app.route('/start_exam/<category>')
def start_exam(category):
    questions = Question.query.filter_by(category=category).all()
    return {'questions': [q.content for q in questions]}

@app.route('/submit_exam', methods=['POST'])
def submit_exam():
    answers = request.json['answers']
    # 评分逻辑
    score = calculate_score(answers)
    return {'score': score}

4. 成绩统计与展示

成绩存储与查询

记录考生成绩,并提供查询接口。

class Score(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    category = db.Column(db.String(50), nullable=False)
    score = db.Column(db.Integer, nullable=False)

@app.route('/save_score', methods=['POST'])
def save_score():
    user_id = session['user_id']
    category = request.form['category']
    score = request.form['score']
    new_score = Score(user_id=user_id, category=category, score=score)
    db.session.add(new_score)
    db.session.commit()
    return 'Score saved successfully'

@app.route('/scores/<user_id>')
def get_scores(user_id):
    scores = Score.query.filter_by(user_id=user_id).all()
    return {'scores': [{'category': s.category, 'score': s.score} for s in scores]}

前端实现

使用HTML表单进行用户注册、登录和题目提交,JavaScript处理异步请求。


<form id="registerForm">
    <input type="text" name="username" placeholder="Username">
    <input type="password" name="password" placeholder="Password">
    <button type="submit">Register</button>
</form>

<script>
document.getElementById('registerForm').addEventListener('submit', function(e) {
    e.preventDefault();
    fetch('/register', {
        method: 'POST',
        body: new

你可能想看:
« 上一篇
下一篇 »

相关推荐

python爬虫教程PDF,Python爬虫书PDF

2026年09月02日

35阅读

外国教程python,python编程教学视频

2026年09月02日

29阅读

python简明教程沈洁元,python详细基础教程

2026年09月02日

24阅读

python库包教程,python 包

2026年09月02日

30阅读

python教程大全集,python教程600集

2026年09月02日

29阅读

python入门教程完整版,python零基础入门全套教程视频

2026年09月02日

26阅读