Introduction
We'll build the Django Quiz Application in this article. In general, The Django Quiz App is an adaptable and dynamic online tool created to transform education and evaluation. It was developed to meet the demand for dynamic and interesting online tests and provides schools, companies, and individuals with an easy-to-use platform to design, administer, and administer tests on a range of topics. With the help of this software, traditional ways of learning and testing will be transformed into something more efficient, fun, and supportive of lifelong learning.
Setting Up the Django Project
To begin, ensure that you have Python and Django installed on your system. You can install Django using pip, the Python package manager. Once installed, create a new Django project by running the following command:
django-admin startproject quiz
Next, navigate to the project directory and create a new Django app -
cd quiz
python manage.py startapp home
Now add this app to the ‘settings.py’
File Structure

Then, we register our app in the settings.py file in the installed_apps sections shown below.
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"home",
"django_extensions",
]
Setting up necessary Files
models.py
The code defines Django models for a quiz application. It includes models for categories, questions, and answers, along with a common base model for shared fields like timestamps. These models are used to organize and store quiz-related data in a Django project.
from django.db import models
import uuid
import random
# Create your models here.
class BaseModel(models.Model):
uid=models.UUIDField(primary_key=True,default=uuid.uuid4,editable=False)
created_at=models.DateField(auto_now_add=True)
updated_at= models.DateField(auto_now_add=True)
class Meta:
abstract=True
class Category(BaseModel):
category_name=models.CharField(max_length=100)
def __str__(self) -> str:
return self.category_name
class Question(BaseModel):
category=models.ForeignKey(Category, related_name='category',on_delete=models.CASCADE)
question=models.CharField(max_length=100)
marks=models.IntegerField(default=5)
def __str__(self) -> str:
return self.question
def get_answers(self):
answer_objs=list(Answer.objects.filter(question = self))
random.shuffle(answer_objs)
data=[]
for answer_obj in answer_objs:
data.append({
'answer' : answer_obj.answer,
'is_correct' : answer_obj.is_correct
})
return data
class Answer(BaseModel):
question=models.ForeignKey(Question, related_name='question_answer',on_delete=models.CASCADE)
answer=models.CharField(max_length=100)
is_correct=models.BooleanField(default=False)
def __str__(self) -> str:
return self.answer
- The BaseModel serves as a foundation for our other models, defining common fields such as uid (a universally unique identifier), created_at, and updated_at. By setting abstract=True, we indicate that this model should not be directly instantiated but serves as a blueprint for other models to inherit from.
- The Category model represents different categories or topics for our quiz questions. It contains a field category_name to store the name of the category. The __str__ method is overridden to provide a human-readable representation of the category.
- The Question model represents individual questions in our quiz. It includes fields for the question itself, its associated category, and the marks allotted to it. Additionally, it provides a method get_answers() to fetch answers related to this question. This method shuffles the answers to ensure randomness and returns a list of answer objects along with their correctness status.
- The Answer model stores possible answers to quiz questions. It maintains a reference to the associated question (question) and stores the answer text (answer). The is_correct field indicates whether the answer is correct or not. The __str__ method returns the answer text for readability.
Run these commands to apply the migrations.
python3 manage.py makemigrations
python3 manage.py migrate
views.py
The code is a part of a Django web application for quizzes.
- home Function: Displays a list of quiz categories. Redirects to the quiz page for a selected category.
- quiz Function: Displays a quiz page for a specific category.
- get_quiz Function: Retrieves random quiz questions, optionally filtered by category. Returns them as JSON. Handles exceptions with a “Something went wrong” response.
from django.http import HttpResponse, JsonResponse from django.shortcuts import render,redirect from .models import * import random # Create your views here. def home(request): context= {'categories': Category.objects.all()} if request.GET.get('category'): return redirect(f"/quiz/?category={request.GET.get('category')}") return render(request,"home.html",context) def quiz(request): context={'category': request.GET.get('category')} return render(request, 'quiz.html',context) def get_quiz(request): try: question_objs= Question.objects.all() if request.GET.get('category'): question_objs=question_objs.filter(category__category_name__icontains=request.GET.get('category')) question_objs=list(question_objs) data=[] random.shuffle(question_objs) for question_obj in question_objs: data.append({ "uid" : question_obj.uid, "category" : question_obj.category.category_name, "question" : question_obj.question, "marks" : question_obj.marks, "answers" : question_obj.get_answers() }) payload={'status' : True, 'data' : data} return JsonResponse(payload) except Exception as e: print(e) return HttpResponse("Something went wrong") - The home view retrieves all available categories from the database and passes them as context to the template home.html. If a category parameter is present in the request's query string, the view redirects the user to the /quiz/ URL with the selected category. Otherwise, it renders the home page template.
- The quiz view renders the quiz page template (quiz.html). It retrieves the selected category from the query string parameters and passes it as context to the template. This view is responsible for displaying the quiz interface to the user.
- The get_quiz view is responsible for retrieving quiz questions from the database and returning them as JSON data. It first fetches all questions and filters them based on the selected category (if provided). The questions are then randomized using random. shuffle() to ensure variety. For each question, relevant information including UID, category, question text, marks, and answers is collected into a dictionary and added to the data list. Finally, a JSON response containing the quiz data is returned to the client.
admin.py
Here we are registering the models.








Join the conversation! Your thoughts help the community grow.