How to get multiple values from a template and update multiple fields in a rows of thesame table in django

39 views
Skip to first unread message
Message has been deleted

AI

unread,
Nov 14, 2018, 9:23:41 AM11/14/18
to Django users
I am working on a result processing website and I've been trying to update multiple fields in each row of table. User should be able to input updated CA Score and EXAM Score values and it should update each student's CA Score and EXAM Score values which has been submitted. image of page

Someone ask same question here trying to update a single field. I tried to work with the solution provided there but was unable to make it suit my need

template

   
<form action="" method="post"> {% csrf_token %}
    {% for student in students %}
   
<tr>
     
<td>{{ student.id_number }}</td>
     
<td>
       
<input type="number" value="{{ student.ca }}" name="student_{{ student.id }}">
     
</td>
     
<td>
       
<input type="number" value="{{ student.exam }}" name="student_{{ student.id }}">
     
</td>
   
</tr>
    {% endfor %}
   
<tr>
     
<td><input type="submit" value="Save"></td>
   
</tr>
   
</tbody>
   
</table>
   
</form>



view

   
def add_score_for(request, id):
       
if request.method == 'GET':
            students
= TakenCourse.objects.filter(course__allocated_course__lecturer__pk=request.user.id).filter(course__id=id)
            context
= { "students":students}
           
return render(request, 'result/add_score_for.html', context)

       
if request.method == 'POST':
            data
= request.POST.dict()
            data
.pop('csrfmiddlewaretoken', None)
           
for i in data.items():
                obj
= TakenCourse.objects.get(id=i[0].split("_")[1])
               
if not str(obj.ca) == str(i[1]): # if i do i[2] trying to get for exams it raise 'tuple index out of range'
                    obj
.ca = i[1]
                    obj
.save()

Thanks in advance.

Reply all
Reply to author
Forward
0 new messages