用Django1.x写一个考试系统
时间: 2024-04-26 15:24:24 浏览: 144
好的,下面是一个简单的考试系统的Django项目示例。
1. 创建Django项目
首先,我们需要使用Django创建一个新项目。在终端中运行以下命令:
```
django-admin startproject exam_system
```
2. 创建应用程序
接下来,我们需要创建一个名为“exam”的应用程序。在终端中运行以下命令:
```
python manage.py startapp exam
```
3. 编写模型
在“exam/models.py”文件中,我们定义了两个模型:一个是“Quiz”,一个是“Question”,其中“Quiz”模型与“Question”模型之间是一对多的关系。
```
from django.db import models
class Quiz(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
class Question(models.Model):
quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE)
text = models.CharField(max_length=200)
answer = models.CharField(max_length=200)
```
4. 编写视图
在“exam/views.py”文件中,我们定义了三个视图函数:一个是“quiz_list”,用于显示所有的考试;一个是“quiz_detail”,用于显示某个考试的详情;另一个是“take_quiz”,用于让用户参加某个考试并提交答案。
```
from django.shortcuts import render, get_object_or_404
from .models import Quiz, Question
def quiz_list(request):
quizzes = Quiz.objects.all()
return render(request, 'exam/quiz_list.html', {'quizzes': quizzes})
def quiz_detail(request, quiz_id):
quiz = get_object_or_404(Quiz, pk=quiz_id)
questions = quiz.question_set.all()
return render(request, 'exam/quiz_detail.html', {'quiz': quiz, 'questions': questions})
def take_quiz(request, quiz_id):
quiz = get_object_or_404(Quiz, pk=quiz_id)
questions = quiz.question_set.all()
if request.method == 'POST':
score = 0
for question in questions:
answer = request.POST.get(str(question.id))
if answer == question.answer:
score += 1
return render(request, 'exam/quiz_result.html', {'score': score})
return render(request, 'exam/take_quiz.html', {'quiz': quiz, 'questions': questions})
```
5. 编写URL模式
在“exam/urls.py”文件中,我们定义了三个URL模式,分别对应于上面的三个视图函数。
```
from django.urls import path
from . import views
app_name = 'exam'
urlpatterns = [
path('', views.quiz_list, name='quiz_list'),
path('<int:quiz_id>/', views.quiz_detail, name='quiz_detail'),
path('<int:quiz_id>/take/', views.take_quiz, name='take_quiz'),
]
```
6. 编写模板
最后,我们需要编写模板文件来渲染视图。在“exam/templates/exam”文件夹中,我们需要创建以下四个模板文件:
- “quiz_list.html”:显示所有的考试列表
- “quiz_detail.html”:显示某个考试的详情
- “take_quiz.html”:让用户参加某个考试并提交答案
- “quiz_result.html”:显示考试结果(即用户得分)
下面是一个简单的示例模板文件:
quiz_list.html:
```
{% extends 'base.html' %}
{% block content %}
<h1>Quiz List</h1>
<ul>
{% for quiz in quizzes %}
<li><a href="{% url 'exam:quiz_detail' quiz.id %}">{{ quiz.title }}</a></li>
{% endfor %}
</ul>
{% endblock %}
```
quiz_detail.html:
```
{% extends 'base.html' %}
{% block content %}
<h1>{{ quiz.title }}</h1>
<p>{{ quiz.description }}</p>
<ul>
{% for question in questions %}
<li>{{ question.text }}</li>
<form method="post" action="{% url 'exam:take_quiz' quiz.id %}">
{% csrf_token %}
<input type="text" name="{{ question.id }}">
{% endfor %}
<button type="submit">Submit</button>
</form>
</ul>
{% endblock %}
```
take_quiz.html:
```
{% extends 'base.html' %}
{% block content %}
<h1>{{ quiz.title }}</h1>
<ul>
{% for question in questions %}
<li>{{ question.text }}</li>
<form method="post" action="{% url 'exam:take_quiz' quiz.id %}">
{% csrf_token %}
<input type="text" name="{{ question.id }}">
{% endfor %}
<button type="submit">Submit</button>
</form>
</ul>
{% endblock %}
```
quiz_result.html:
```
{% extends 'base.html' %}
{% block content %}
<h1>Your score is {{ score }} out of {{ questions.count }}.</h1>
{% endblock %}
```
7. 运行Django项目
最后,我们需要在终端中运行以下命令来启动Django项目:
```
python manage.py runserver
```
然后,我们就可以在浏览器中访问“http://localhost:8000/”来查看考试系统了。
阅读全文