user12289803
user12289803

Reputation:

How to use Get List from POST Data - Django

i hope the title is enough to know what is my problem

enter image description here

This is my code in html

when I tried to save this to my database only the "6" is save just like in the picture

enter image description here

I use jquery to generate the date and textbox to input the grade

this is my html

<table  id="blacklistgrid" border="2px">
      <tr>
          <th id="th">Students Name</th>
          <th data-id='headers' id='header'>Average</th>
      </tr>
      {% for student in teacherStudents %}
      <tr class="tr2">
          <td class="td" ><input type="text" name="students" value="{{student.id}}" id="student" >{{student.Students_Enrollment_Records.Student_Users}}</td>
           <td data-id='row' id="ans"><input type='number' class='averages'  readonly/></td>
      </tr>
      {% endfor %}
</table>

<script>
     var counter = 0;
    function doTheInsert(){

        let header=$("tr#tr");  // same as $.find("tr[id='tr2']")
        $('#th').after("<th data-id='headers' id='header'><input type='date' name='date'></th>");
        var rows=$(".tr2");
        $("<td data-id='row' ><input type='number' name='gradeko' class='average' /></td>").insertAfter(".td");
        counter++;
    }
    </script>

this is my views.py

for gradeko in request.POST.get('gradeko'):
    pass

for students in request.POST.getlist('students'):
    studentss = StudentsEnrolledSubject(id=students)
    date = request.POST.getlist('date')

    V_insert_data = studentsEnrolledSubjectsGrade(
        Teacher=teacher,
        Students_Enrollment_Records=studentss,
        Date=date,
        Grade=gradeko
    )
    V_insert_data.save()

this is my model.py

class studentsEnrolledSubjectsGrade(models.Model):
    Teacher = models.ForeignKey(EmployeeUser, related_name='+', on_delete=models.CASCADE,
                                                null=True,blank=True)
    Subjects = models.ForeignKey(Subject, related_name='+', on_delete=models.CASCADE, null=True)
    Students_Enrollment_Records = models.ForeignKey(StudentsEnrolledSubject, related_name='+',on_delete=models.CASCADE, null=True)
    Grading_Categories = models.ForeignKey(gradingCategories, related_name='+', on_delete=models.CASCADE,
                                                null=True,blank=True)
    Date = models.DateField(null=True, blank=True)
    Grade = models.FloatField(null=True, blank=True)

what i tried

global gradekos, dates
    for gradeko in request.POST.getlist('gradeko'):
    gradekos = gradeko
    pass
    print(gradekos)
for date in request.POST.getlist('date'):
    dates = date
    pass
for students in request.POST.getlist('students'):
    studentss = StudentsEnrolledSubject(id=students)
    #date = request.POST.get('date')

    V_insert_data = studentsEnrolledSubjectsGrade(
        Teacher=teacher,
        Students_Enrollment_Records=studentss,
        Date=dates,
        Grade=gradekos
    )
    V_insert_data.save()

UPDATE when i tried two dates, only 1 date save in the database

enter image description here

enter image description here

for example

enter image description here

this is what I want result

enter image description here

this is the actual result

enter image description here

Upvotes: 2

Views: 4552

Answers (1)

Linh Nguyen
Linh Nguyen

Reputation: 3890

you setting gradekos = gradeko so it just override gradekos with your previous value(that go for dates too). So you need get all values of each object inside an array and loop through it to get the actual position values

gradekos = []
dates = []
for gradeko in request.POST.getlist('gradeko'):
    gradekos.append(gradeko)
for date in request.POST.getlist('date'):
    dates.append(date)

i=0
for single_date in dates:

   for student in request.POST.getlist('students'):
      students = StudentsEnrolledSubject(id=student)

      V_insert_data = studentsEnrolledSubjectsGrade(
      Teacher=teacher,
      Students_Enrollment_Records=students,
      Date=single_date,
      Grade=gradekos[i]
      )
      V_insert_data.save()
      i += 1

Upvotes: 1

Related Questions