DEV Community

loading...
Cover image for Exam Result Website | A Django Project

Exam Result Website | A Django Project

Madhuban Khatri
I am Madhuban Khatri. Skills : Python , Php and JavaScript | Fun Fact : I am a Self - Taught Programmer |
・7 min read

Hi,
New Project is available here.

Project Details

  1. Name = Exam Result Website
  2. Framework = Django (Python Framework)
  3. About = Students can check their Exam Result using Roll Number and Admin can add new Students and edit/delete students data.
  4. Available on = DEV , GitHub and YouTube

Source Code

Create A New Project using this Command

django-admin startproject exam_result
Enter fullscreen mode Exit fullscreen mode

Create A New App in Exam_Result Project

cd exam_result
python manage.py startapp main
Enter fullscreen mode Exit fullscreen mode

Add your Main app in Settings.py file, like this

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'main'  #Add app here
]
Enter fullscreen mode Exit fullscreen mode

Configure your Project's urls.py file

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('main.urls'))
]
Enter fullscreen mode Exit fullscreen mode

Create Models in models.py

from django.db import models

# Create your models here.
class Student(models.Model):
    name = models.CharField(max_length=100)
    roll_no = models.IntegerField()
    hindi = models.IntegerField(default=0)
    english = models.IntegerField(default=0)
    maths = models.IntegerField(default=0)
    science = models.IntegerField(default=0)
    total = models.IntegerField(default=0)
    perecent = models.DecimalField(max_digits=5, decimal_places=2)

    def __str__(self):
        return self.name
Enter fullscreen mode Exit fullscreen mode

Register your models in admin.py, like this

from django.contrib import admin
from .models import Student

# Register your models here.
admin.site.register(Student)
Enter fullscreen mode Exit fullscreen mode

Run these important Commands in Terminal

python manage.py makemigrations
python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

views.py

from django.http.response import HttpResponse
from django.shortcuts import render, redirect
from django.http import HttpResponse
from .models import Student

# Create your views here.
def index(request):
    return render(request, 'index.html')


def result(request):
    if request.method == 'POST':
        rollNo = int(request.POST['roll_no'])
        student = Student.objects.get(roll_no=rollNo)
        hindi = student.hindi
        english = student.english
        maths = student.maths
        science = student.science
        total = hindi+english+maths+science
        percent = total/400*100
        if(percent<33):
            status = 'Fail'
        else:
            status = 'Pass'
        params = {
            'roll_no': rollNo,
            'name': student.name,
            'hindi': hindi,
            'english': english,
            'maths': maths,
            'science': science,
            'total': total,
            'percent': percent,
            'status': status
        }
        # print('get method', student.name)
        return render(request, 'result.html', params)
    else:
        print('get method')
    return render(request, 'result.html')



def admin_login(request):
    if 'user' in request.session:
        return render(request, 'admin_panel.html')
    else:
        return render(request, 'admin-login.html')



def admin_panel(request):
    if 'user' in request.session:
        students = Student.objects.all()
        params = {'students': students}
        return render(request, 'admin_panel.html',params)
    else:
        if request.method == 'POST':
            user_name = request.POST['uname']
            pass_word = request.POST['pwd']
            if user_name == 'admin' and pass_word == 'admin123':
                request.session['user'] = user_name
                students = Student.objects.all()
                params = {'students': students}
                return render(request, 'admin_panel.html', params)
            else:
                return render(request, 'admin-login.html')
        else:
            return render(request, 'admin-login.html')


def delete_student(request, id):
    get_stu = Student.objects.get(id=id)
    get_stu.delete()
    return redirect('/admin_panel')


def edit_student(request, id):
    get_stu = Student.objects.get(id=id)
    params = {'student': get_stu}
    return render(request, 'edit.html', params)


def edit_confirm(request, id):
    if request.method == 'POST':
        get_stu = Student.objects.get(id=id)
        get_stu.name = request.POST['sname']
        get_stu.roll_no = request.POST['roll-no']
        get_stu.hindi = request.POST['hindi']
        get_stu.english = request.POST['english']
        get_stu.maths = request.POST['maths']
        get_stu.science = request.POST['science']
        total = int(request.POST['hindi'])+int(request.POST['english'])+int(request.POST['maths'])+int(request.POST['science'])
        get_stu.total = total
        get_stu.perecent = total/4
        get_stu.save()
        return redirect('/admin_panel')
    else:
        return redirect('/admin_login')

def admin_logout(request):
    del request.session['user']
    return redirect('/')



def add_student(request):
    return render(request, 'add_student.html')


def add_confirm(request):
    if request.method == 'POST':
        sname = request.POST['sname']
        roll_no = request.POST['roll-no']
        hindi = int(request.POST['hindi'])
        english = int(request.POST['english'])
        maths = int(request.POST['maths'])
        science = int(request.POST['science'])
        total = hindi+english+maths+science
        percent = total/400*100
        add_student = Student.objects.create(name=sname,roll_no=roll_no,
                        hindi=hindi,english=english,maths=maths,science=science,
                        total=total,perecent=percent)
        add_student.save()
        return redirect('/admin_panel')
    else:
        return redirect('/admin_panel')

Enter fullscreen mode Exit fullscreen mode

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('result/', views.result, name='result'),
    path('admin_login/', views.admin_login, name='admin-login'),
    path('admin_panel/', views.admin_panel, name='admin-panel'),
    path('delete-student/<int:id>/', views.delete_student, name='delete-student'),
    path('edit-student/<int:id>/', views.edit_student, name='edit-student'),
    path('edit-confirm/<int:id>/', views.edit_confirm, name='edit-confirm'),
    path('logout/', views.admin_logout, name='admin-logout'),
    path('add_student/', views.add_student, name='add_student'),
    path('add_confirm/', views.add_confirm, name='add_confirm'),
]
Enter fullscreen mode Exit fullscreen mode

Templates

base.html

<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">

    <title>{% block title %}{% endblock %}</title>
</head>

<body>
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
        <div class="container-fluid">
            <a class="navbar-brand" href="#">Exam Result</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse"
                data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false"
                aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                    <li class="nav-item">
                        <a class="nav-link active" aria-current="page" href="{% url 'index' %}">Home</a>
                    </li>
                    <li class="nav-item">
                        {% if 'user' in request.session %}
                            <a class="nav-link btn btn-primary text-white" href="{% url 'admin-logout' %}">Logout</a>
                        {% else %}
                            <a class="nav-link btn btn-primary text-white" href="{% url 'admin-login' %}">Admin Panel</a>
                        {% endif %}
                    </li>

                </ul>

            </div>
        </div>
    </nav>

    {% block body %}

    {% endblock %}

    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4"
        crossorigin="anonymous"></script>

</body>

</html>
Enter fullscreen mode Exit fullscreen mode

index.html

{% extends 'base.html' %}

{% block title%}
    Home   
{% endblock %}

{% block body %}
<div class="container w-25">
    <form action="{% url 'result' %}" method="post">
        {% csrf_token %}
    <div class="mb-3 my-5">
        <label for="roll_no" class="form-label">Roll Number</label>
        <input type="text" class="form-control" name="roll_no" id="roll_no">
    </div>
    <div class="mb-3">
        <input type="submit" value="See Result" class="btn btn-success"/>
    </div>
    </form>
</div>
{% endblock %}
Enter fullscreen mode Exit fullscreen mode

result.html

{% extends 'base.html' %}


{% block title%}
    Result
{% endblock %}

{% block body %}
<table class="table my-4">
    <thead>
        <tr>
            <th scope="col">Roll No</th>
            <th scope="col">Name</th>
            <th scope="col">Hindi</th>
            <th scope="col">English</th>
            <th scope="col">Maths</th>
            <th scope="col">Science</th>
            <th scope="col">Total Marks</th>
            <th scope="col">Percentage</th>
            <th scope="col">Status</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th scope="row">{{roll_no}}</th>
            <td>{{name}}</td>
            <td>{{hindi}}</td>
            <td>{{english}}</td>
            <td>{{maths}}</td>
            <td>{{science}}</td>
            <td>{{total}}</td>
            <td>{{percent}}</td>
            {% if status == 'Pass' %}
                <td><p class="btn btn-success">{{status}}</p></td>
            {% else %}
            <td><p class="btn btn-danger">{{status}}</p></td>
            {% endif %}

        </tr>

    </tbody>
</table>
{% endblock %}
Enter fullscreen mode Exit fullscreen mode

admin-login.html

{% extends 'base.html' %}

{% block title%}
    Admin Login    
{% endblock %}

{% block body %}
<div class="container w-50 my-4">
    <form action="{% url 'admin-panel' %}" method="post">
        {% csrf_token %}
    <div class="mb-3">
        <label for="username" class="form-label"><b>Username</b></label>
        <input type="text" class="form-control" id="username" name="uname">
    </div>
    <div class="mb-3">
        <label for="password" class="form-label"><b>Password</b></label>
        <input type="password" class="form-control" id="password" name="pwd">
    </div>
    <div class="mb-3">
        <input type="submit" class="btn btn-success" value="Login">
    </div>
    </form>
</div>
{% endblock %}
Enter fullscreen mode Exit fullscreen mode

admin_panel.html

{% extends 'base.html' %}

{% block title%}
    Admin Panel    
{% endblock %}

{% block body %}
<div class="container-fluid my-3">
    <a href="{% url 'add_student' %}" class="btn btn-warning">Add Student</a>
</div>
<table class="table table-primary my-4 table-hover">
    <thead>
        <tr>
            <th scope="col">Roll No.</th>
            <th scope="col">Name</th>
            <th scope="col">Hindi</th>
            <th scope="col">English</th>
            <th scope="col">Maths</th>
            <th scope="col">Science</th>
            <th scope="col">Total Marks</th>
            <th scope="col">Precentage</th>
            <th scope="col">Operations</th>

        </tr>
    </thead>
    <tbody>
        {% for s in students %}
        <tr>
            <th scope="row">{{s.roll_no}}</th>
            <td>{{s.name}}</td>
            <td>{{s.hindi}}</td>
            <td>{{s.english}}</td>
            <td>{{s.maths}}</td>
            <td>{{s.science}}</td>        
            <td>{{s.total}}</td>
            <td>{{s.perecent}}</td>
            <td>


                <a href="{% url 'edit-student' s.id %}" class="btn btn-small btn-primary" id='{s.id}'>Edit</a>


                <a href="{% url 'delete-student' s.id %}" class="btn btn-small btn-danger" id='{s.id}'>Delete</a>

            </td>

        </tr>
        {% endfor %}
    </tbody>
</table>

{% endblock %}
Enter fullscreen mode Exit fullscreen mode

edit.html

{% extends 'base.html' %}

{% block title%}
Edit Panel
{% endblock %}

{% block body %}
<div class="container w-25 my-3">
    <div class="mb-3">
        <h4>Edit</h4>
    </div>
    <form action="{% url 'edit-confirm' student.id %}" method="post">
        {% csrf_token %}
        <div class="mb-3">
            <label for="name" class="form-label">Student Name</label>
            <input type="text" class="form-control" name="sname" value='{{student.name}}'>
        </div>
        <div class="mb-3">
            <label for="roll-no" class="form-label">Roll Number</label>
            <input type="text" class="form-control" name="roll-no" value='{{student.roll_no}}'>
        </div>
        <div class="mb-3">
            <label for="hindi" class="form-label">Hindi</label>
            <input type="text" class="form-control" name="hindi" value='{{student.hindi}}'>
        </div>
        <div class="mb-3">
            <label for="english" class="form-label">English</label>
            <input type="text" class="form-control" name="english" value='{{student.english}}'>
        </div>
        <div class="mb-3">
            <label for="maths" class="form-label">Maths</label>
            <input type="text" class="form-control" name="maths" value='{{student.maths}}'>
        </div>
        <div class="mb-3">
            <label for="science class=" form-label">Science</label>
            <input type="text" class="form-control" name="science" value='{{student.science}}'>
        </div>

        <input type="submit" class="form-control btn btn-success" value="Edit">

    </form>
</div>
{% endblock %}
Enter fullscreen mode Exit fullscreen mode

add_student.html

{% extends 'base.html' %}

{% block title %}
Add Student
{% endblock %}

{% block body %}
<div class="container w-25">
    <h3>Add Student</h3>
    <form action="{% url 'add_confirm' %}" method="post">
        {% csrf_token %}
        <div class="mb-3">
            <label for="name" class="form-label">Student Name</label>
            <input type="text" class="form-control" name="sname">
        </div>
        <div class="mb-3">
            <label for="roll-no" class="form-label">Roll Number</label>
            <input type="text" class="form-control" name="roll-no" >
        </div>
        <div class="mb-3">
            <label for="hindi" class="form-label">Hindi</label>
            <input type="number" class="form-control" name="hindi">
        </div>
        <div class="mb-3">
            <label for="english" class="form-label">English</label>
            <input type="number" class="form-control" name="english">
        </div>
        <div class="mb-3">
            <label for="maths" class="form-label">Maths</label>
            <input type="number" class="form-control" name="maths">
        </div>
        <div class="mb-3">
            <label for="science class=" form-label">Science</label>
            <input type="number" class="form-control" name="science">

        </div>

        <input type="submit" class="form-control btn btn-success" value="Add">

    </form>
</div>
{% endblock %}
Enter fullscreen mode Exit fullscreen mode

That's It.
If you face any problem related to this project you can message me and we will solve the problem.

Code Is Also Available On GITHUB

Discussion (0)