Not able to save data from aDjango form: Django 4.1, Windows 10

I am working on a small student application. I designed a table for storing students' information in which those fields of information are requested at the first registration and those can be updated later or updated during the process. For example, a student's information can update in the process by putting on-hold (update the onhold field) in case that student has been absent for 3 weeks. Below is my code.


###Model.py

from django.db import models
from django.contrib import admin
from django.utils.translation import gettext_lazy as _


class Student(models.Model):
    #std_matricule = models.CharField(verbose_name='Student matricule', max_length=6, null=False, unique=True, primary_key=True)
    std_matricule = models.CharField(verbose_name='Matricule', max_length=6, null=False, unique=True, blank=False, help_text='Matricule of the student')
    std_parents = models.ForeignKey(Parents, on_delete=models.DO_NOTHING, related_name='Parents', unique=False, default=1, verbose_name='Student parents')
    std_email = models.EmailField(verbose_name='Email', null=False, blank=True, help_text='Enter the email of the student or leave blank if not exist')
    std_password = models.CharField(verbose_name='Password', max_length=64, null=False, blank=True, help_text='Type the password with 6 characters minimum')
    std_surname = models.CharField(verbose_name='Surname', null=False, blank=False, max_length=64, help_text='Type the Surname of the student as in the birth certificate')
    std_firstname = models.CharField(verbose_name='First name', null=False, blank=True, max_length=64, help_text='Type the student first name')
    std_nickname = models.CharField(verbose_name='Student Nickname', max_length=64, null=False, blank=True, help_text='If exist, type student nickname here')
    class Sex(models.TextChoices):
        MALE = 'Male', _('Male')
        FEMALE = 'Female', _('Female')
    std_sex = models.CharField(
        max_length=8,
        choices=Sex.choices,
        default=Sex.MALE,
        verbose_name='Sex',
        help_text='Select the sex from the dropdown list',
    )
    std_dateOfBirth = models.DateField(verbose_name='Date of birth', null=False, blank=False, help_text='Birth date of the student as in the birth certificate')
    std_placeOfBirth = models.CharField(verbose_name='Place of birth', max_length=64, null=False, blank=False, help_text='Place of birth of the student as in the birth certificate')
    std_countryOfBirth = models.CharField(verbose_name='Country of birth', max_length=64, null=True, blank=True, help_text='Country of birth of the student as in the birth certificate')
    std_nationality = models.CharField(verbose_name='Student nationality', max_length=64, null=True, blank=True, help_text='Nationality of the student')
    std_adress = models.CharField(verbose_name='Student adress', max_length=256, null=False, blank=True)
    std_phoneNumber = models.CharField(verbose_name='Student phone number', max_length=32, null=False, blank=True, help_text='Student phone number')
    std_enrolmentDate = models.DateField(verbose_name='Student enrolment date', null=False, blank=False, help_text='Enter the enrolment date')
    std_photo = models.ImageField(verbose_name='Student photo', null=False, help_text='Please, upload a recent photo of the student')
    std_birthCertificate = models.FileField('Student birth certificate scann', null=False, blank=True, help_text='Upload a readable scan copy of the birth certificate here')
    std_otherDocs = models.FileField('Student others documents', null=False, blank=True, help_text='If exist, upload those support documents')
    class STD_status(models.TextChoices):
        ACTIF = 'Active', _('Active')
        INACTIF = 'Inactive', _('Inactive')
        ONHOLD = 'Onhold', _('Onhold')
        COMPLETED_P ='Completed Primary', _('Completed Primary')
        COMPLETED_S ='Completed Secondary', _('Completed Secondary')
    std_status = models.CharField(
        max_length=32,
        choices=STD_status.choices,
        default=STD_status.ACTIF,
        verbose_name='Status',
        help_text='Student status, select from the dropdown list',
    )
    std_deleteStatus = models.BooleanField(verbose_name='Delete status', default=False)
    std_last_loginDate = models.DateField(verbose_name='Last login date', null=True, blank=True, help_text='Last login date')
    std_last_loginIP = models.CharField(verbose_name='Last login IP', max_length=64, null=True, blank=True, help_text='Last login IP')
    std_createDate = models.DateField(verbose_name='Create date', auto_now=True)
    std_modifyDate = models.DateField(verbose_name='Modify date', auto_now=True)

    def __str__(self):
        return 'Matricule: ' + self.std_matricule + ' - Full name: ' + self.std_surname + ' ' + self.std_firstname

###view.py


from django.shortcuts import render, redirect
from django.http import HttpResponse, Http404, HttpResponseRedirect, JsonResponse
from django.urls import reverse
from django.contrib import messages
from django.core.files.storage import FileSystemStorage
from django.views.decorators.csrf import csrf_exempt
from students_management_app.forms import StudentRegistrationForm
from students_management_app.models import Student

def addStudentSave(request):
    if request.method == "POST":
        form = StudentRegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('listStudents')
    return render(request, 'students_management_app/student_template/addStudent.html', {'form': form})


def addStudentSave(request):
    if request.method != "POST":
        messages.error(request, "Invalid Method")
        return redirect('addStudent')
    else:
        print(request.POST)
        form = StudentRegistrationForm(request.POST, request.FILES)
        if form.is_valid():
            std_surname = form.cleaned_data['std_surname']
            std_firstname = form.cleaned_data['std_firstname']
            std_nickname = form.cleaned_data['std_nickname']
            std_matricule = form.cleaned_data['std_matricule']
            std_email = form.cleaned_data['std_email']
            std_sex = form.cleaned_data['std_sex'] 
            std_dateOfBirth = form.cleaned_data['std_dateOfBirth']
            std_placeOfBirth = form.cleaned_data['std_placeOfBirth']
            std_countryOfBirth = form.cleaned_data['std_countryOfBirth']
            std_nationality = form.cleaned_data['std_nationality']
            std_adress = form.cleaned_data['std_adress']
            std_phoneNumber = form.cleaned_data['std_phoneNumber']
            std_enrolmentDate = form.cleaned_data['std_enrolmentDate']

            if len(request.FILES) != 0:
                std_photo = request.FILES['std_photo']
                fs = FileSystemStorage()
                filename = fs.save(std_photo.name, std_photo)
                std_photo_url = fs.url(filename)
            else:
                std_photo = None

            try:
                student = Student.objects.create(std_surname=std_surname,
                                                    std_firstname=std_firstname,
                                                    std_nickname=std_nickname,
                                                    std_matricule=std_matricule,
                                                    std_email=std_email,
                                                    std_sex=std_sex,
                                                    std_dateOfBirth=std_dateOfBirth,
                                                    std_placeOfBirth=std_placeOfBirth,
                                                    std_countryOfBirth=std_countryOfBirth,
                                                    std_nationality=std_nationality,
                                                    std_adress=std_adress,
                                                    std_phoneNumber=std_phoneNumber,
                                                    std_enrolmentDate=std_enrolmentDate,
                                                    std_photo=std_photo_url)
                
                student.save()
                messages.success(request, "Student Added Successfully!")
                return redirect('addStudent')
            except:
                messages.error(request, "Failed to Add Student!")
                return redirect('addStudent')
        else:
            return redirect('addStudent')


###form.py

from django import forms
from django.forms import ModelForm
from students_management_app.models import Student

class StudentRegistrationForm(forms.Form):    
    std_surname = forms.CharField(label='Surname', max_length=64, required=True)
    std_firstname = forms.CharField(label='First name',  max_length=64, required=True)
    std_nickname = forms.CharField(label='Student Nickname', max_length=64, required=True)
    std_matricule = forms.CharField(label='Matricule', max_length=6, required=True)
    ###std_parents = models.ForeignKey(Parents, on_delete=models.DO_NOTHING, related_name='Parents', unique=False, default=1, verbose_name='Student parents')
    std_email = forms.EmailField(label='Email')
    gender_list = (
        ('Male','Male'),
        ('Female','Female')
    )
    std_sex = forms.ChoiceField(label='Sex', choices=gender_list)    
    std_dateOfBirth = forms.DateField(label='Date of birth', widget=forms.DateInput(format='%d/%m/%Y'))
    std_placeOfBirth = forms.CharField(label='Place of birth', max_length=64)
    std_countryOfBirth = forms.CharField(label='Country of birth', max_length=64)
    std_nationality = forms.CharField(label='Nationality', max_length=64)
    std_adress = forms.CharField(label='Adress', max_length=256)
    std_phoneNumber = forms.CharField(label='Phone number', max_length=32)
    std_enrolmentDate = forms.DateField(label='Enrolment date', widget=forms.DateInput(format='%d/%m/%Y') )
    std_photo = forms.ImageField(label='Student photo')
    std_birthCertificate = forms.FileField(label='Scan of the Birth certificate')
    std_otherDocs = forms.FileField(label='Student others documents')


###html template

 {% load static %}
    <title>List of the students</title>
</head>
    <body>
        <h1>Add a Student:</h1>
        <form action="" method="POST">
            {%  csrf_token %}
            {{ form.as_p }}
            <input type="submit" value="Save">
        </form>
    </body>
</html>


When I fill out the form and submit it, it opens a new form without safe the data.

Back to Top