Python语言实现考试创建和参加功能
使用Python语言编写代码。我们将实现一个简单的校园考试系统,首先实现用户注册和登录功能。你已经完成了项目的基础配置和运行环境的设置,接下来我们将逐步添加更多功能。
步骤2:实现考试创建和参加功能
我们将分步实现以下功能:
- 创建考试
- 学生参加考试
- 查看考试结果
1. 创建数据库模型
我们需要为考试和问题创建数据库模型。修改你的 app.py
文件,添加以下内容:
from datetime import datetime
class Exam(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
description = db.Column(db.String(200), nullable=True)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
questions = db.relationship('Question', backref='exam', lazy=True)
class Question(db.Model):
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.String(200), nullable=False)
answer = db.Column(db.String(100), nullable=False)
exam_id = db.Column(db.Integer, db.ForeignKey('exam.id'), nullable=False)
class StudentAnswer(db.Model):
id = db.Column(db.Integer, primary_key=True)
student_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
question_id = db.Column(db.Integer, db.ForeignKey('question.id'), nullable=False)
answer = db.Column(db.String(100), nullable=False)
is_correct = db.Column(db.Boolean, nullable=False)
2. 创建考试功能
我们需要创建一个页面供管理员创建考试。首先,添加一个视图函数和模板。
在 app.py
中,添加以下路由和视图函数:
@app.route('/create_exam', methods=['GET', 'POST'])
def create_exam():
if request.method == 'POST':
title = request.form['title']
description = request.form.get('description', '')
exam = Exam(title=title, description=description)
db.session.add(exam)
db.session.commit()
flash('Exam created successfully!', 'success')
return redirect(url_for('index'))
return render_template('create_exam.html')
在 templates
文件夹中创建 create_exam.html
文件,并添加以下内容:
<!DOCTYPE html>
<html>
<head>
<title>Create Exam</title>
</head>
<body>
<h2>Create Exam</h2>
<form method="POST" action="{{ url_for('create_exam') }}">
<label for="title">Title:</label><br>
<input type="text" id="title" name="title" required><br><br>
<label for="description">Description:</label><br>
<textarea id="description" name="description"></textarea><br><br>
<button type="submit">Create Exam</button>
</form>
</body>
</html>
3. 添加问题功能
我们需要创建一个页面供管理员添加问题。首先,添加一个视图函数和模板。
在 app.py
中,添加以下路由和视图函数:
@app.route('/add_question/<int:exam_id>', methods=['GET', 'POST'])
def add_question(exam_id):
exam = Exam.query.get_or_404(exam_id)
if request.method == 'POST':
text = request.form['text']
answer = request.form['answer']
question = Question(text=text, answer=answer, exam=exam)
db.session.add(question)
db.session.commit()
flash('Question added successfully!', 'success')
return redirect(url_for('add_question', exam_id=exam.id))
return render_template('add_question.html', exam=exam)
在 templates
文件夹中创建 add_question.html
文件,并添加以下内容:
<!DOCTYPE html>
<html>
<head>
<title>Add Question</title>
</head>
<body>
<h2>Add Question to {{ exam.title }}</h2>
<form method="POST" action="{{ url_for('add_question', exam_id=exam.id) }}">
<label for="text">Question:</label><br>
<input type="text" id="text" name="text" required><br><br>
<label for="answer">Answer:</label><br>
<input type="text" id="answer" name="answer" required><br><br>
<button type="submit">Add Question</button>
</form>
</body>
</html>
4. 学生参加考试
我们需要创建一个页面供学生参加考试。首先,添加一个视图函数和模板。
在 app.py
中,添加以下路由和视图函数:
@app.route('/take_exam/<int:exam_id>', methods=['GET', 'POST'])
def take_exam(exam_id):
exam = Exam.query.get_or_404(exam_id)
questions = Question.query.filter_by(exam_id=exam_id).all()
if request.method == 'POST':
for question in questions:
answer = request.form.get(f'question_{question.id}')
is_correct = (answer == question.answer)
student_answer = StudentAnswer(student_id=session['user_id'], question_id=question.id, answer=answer, is_correct=is_correct)
db.session.add(student_answer)
db.session.commit()
flash('Exam submitted successfully!', 'success')
return redirect(url_for('index'))
return render_template('take_exam.html', exam=exam, questions=questions)
在 templates
文件夹中创建 take_exam.html
文件,并添加以下内容:
<!DOCTYPE html>
<html>
<head>
<title>Take Exam</title>
</head>
<body>
<h2>{{ exam.title }}</h2>
<form method="POST" action="{{ url_for('take_exam', exam_id=exam.id) }}">
{% for question in questions %}
<p>{{ question.text }}</p>
<input type="text" name="question_{{ question.id }}" required><br><br>
{% endfor %}
<button type="submit">Submit Exam</button>
</form>
</body>
</html>
5. 查看考试结果
我们需要创建一个页面供学生查看考试结果。首先,添加一个视图函数和模板。
在 app.py
中,添加以下路由和视图函数:
@app.route('/results/<int:exam_id>')
def results(exam_id):
exam = Exam.query.get_or_404(exam_id)
student_answers = StudentAnswer.query.filter_by(student_id=session['user_id'], question_id=exam_id).all()
return render_template('results.html', exam=exam, student_answers=student_answers)
在 templates
文件夹中创建 results.html
文件,并添加以下内容:
<!DOCTYPE html>
<html>
<head>
<title>Exam Results</title>
</head>
<body>
<h2>Results for {{ exam.title }}</h2>
{% for answer in student_answers %}
<p>Question: {{ answer.question.text }}</p>
<p>Your answer: {{ answer.answer }}</p>
<p>Correct: {{ answer.is_correct }}</p>
<hr>
{% endfor %}
</body>
</html>
总结
我们已经添加了基本的考试创建、添加问题、参加考试和查看结果的功能。