[Answered ]-Django pass data from HTML table to database model

1👍

@Sahil Mohile- Your model needs some clean-up.

class StudentAttendance(models.Model):
    attendance_id = models.AutoField(primary_key= True)
    student = models.ForeignKey(CustomStudent, on_delete=CASCADE,db_index=True)
    attendace = models.CharField(max_length = 20, choices=AttendenceChioces) 
    date = models.DateField(auto_now= True)
    teacher = models.ForeignKey(settings.AUTH_USER_MODEL)#Dont call it #tname, explicit is better than implicit. 
    
    def __str__(self):
        return str(self.sname)
    

Now. my main point. Attendance record is tied to a Term/Semester. How about you
create a Semester Table. Tie the student attendance with a specific semester/term

import datetime
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models


def current_year():
    return datetime.date.today().year


def max_value_current_year(value):
    return MaxValueValidator(current_year())(value)


  class Semester(models.Model):
        name = models.CharField(max_length=50)
        year = models.PositiveIntegerField(
        default=current_year(), validators=[MinValueValidator(1984), max_value_current_year])
        .........
   e.g Fall 2020



 class StudentAttendance(models.Model):
        attendance_id = models.AutoField(primary_key= True)
        semester = models.ForeignKey(Semester,db_index=True)
        student = models.ForeignKey(CustomStudent, on_delete=CASCADE,db_index=True)
        attendace = models.CharField(max_length = 20, choices=AttendenceChioces) 
        date = models.DateField(auto_now= True)
        teacher = models.ForeignKey(settings.AUTH_USER_MODEL)#Dont call it #tname, explicit is better than implicit. 

Also, I predict that there might be a situation where you will be asked to present the number of present days for a specific student in a specific semester. Something to think about.


I am a bit confused. You are capturing the data but where are you making the ajax POST? You need to share the POST request

var formData = new FormData()
        $(document).ready(function(){

            $("#checked").click('click', function(){
                $students = document.getElementsByClassName('student-name').textContent;
                $attendance = $('#attendance').val()

                console.log($students)
                console.log($attendance)
            })
        })
 

Shouldn’t you be doing the following too

  $.ajax({
            type: "POST",
            url: 'your url',
            data: {
                 #your data
                csrfmiddlewaretoken: csrf_token
            },     
           success:function(response){
          },
          error:function(error){
            }
});    
👤SDRJ

Leave a comment