在 Django 模板中将多个参数传递给 URL 的竞争条件

race condition on passing multiple parameter into URL in Django templates

观看次数

def increase_att(request,student_id ,sub):
    print(sub)
    print(subject)
    t = StudentInfo.objects.get(id=student_id)
    t.sub1_att = F('sub1_att')+1
    t.save()
    return HttpResponseRedirect('count?subject=Python')

模板

<div class="card">
    <div class="card-header">
        <h1>Attendance Count</h1>
    </div>
    <div class="card-body">
        <div class="row">
            <div class="col-md-6">
                <form action="" method="GET">
                    Subject: <input type="text" name="subject" class="form-control">
                    <input type="submit" value="Submit" class="btn btn-primary" >
                </form>
            </div>
        </div>
    </div>
</div>

{% if student_list_sub1 %} <!-- for 1st subject--> 
    <table class="table table-responsive-sm table-bordered table-striped table-sm">
        <tr>
            <th>Name</th>
            <th>Admission ID</th>
            <th>{% for std_list in student_list_sub1 %}
                {{std_list.sub1}} 
                {% endfor %} Attendance</th>
            <th>Action</th>
        </tr>
        {% for std_list in student_list_sub1 %}
            <tr>
                <td class="cls_name">{{ std_list.name }}</td>
                <td class="cls_admission">{{ std_list.usn }}</td>
                <td class="cls_class">{{ std_list.sub1_att }}%</td>
                <td><button type="submit" onclick="location.href='{% url 'increase_att' std_list.id std_list.sub1 %}'">Present</button> <button class="count_btn">Absent</button></td>
                
            </tr>
        {% endfor %}
    </table>
{% endif %}

urls.py

path('attendance/<student_id>/<sub>',views.increase_att,name='increase_att'),

使用单个参数“student_id”程序运行良好,即 在按下按钮(Present)时,出席率 increments.But 在传递多个值(如“student_id”和“sub”时,它面临竞争条件。

models.py

class StudentInfo(models.Model):
    admission_date = models.DateField()
    usn = models.CharField(max_length=10)
    name = models.CharField(max_length=100)
    sub1 = models.ForeignKey(StudentSubjectInfo,on_delete=CASCADE,related_name='sub1',null=True)
    sub1_att = models.IntegerField(null=True,default=0)
    sub2 = models.ForeignKey(StudentSubjectInfo,on_delete=CASCADE,related_name='sub2',null=True)
    sub2_att = models.IntegerField(null=True,default=0)```

i have put subject name and its attendance for each students.
Upon displaying the list of students i have also put a button to each student upon pressing that button "increase_att" should be called and attendance of that student(student_id) in that particular subject(sub) must be incremented.

我必须承认你的代码中有很多不规范之处,这就是我试图想出的办法来帮助你朝着正确的方向前进。读你的代码我差点失去理智。但我希望这会导致解决方案。您的 return HttpResponseRedirect('count?subject=Python') 是另外一回事。我认为您应该为了清晰起见而重构您的代码,这样其他人就可以提供帮助。但我希望这会导致您的解决方案

def increase_att(request,student_id):
    # This where we get the id of the student 
    student = get_object_or_404(StudentInfo,id=student_id)
    # Am assuming if you have a model for subject
    # Here we are filtering subject by student id 
    subject = StudentSubjectInfo.objects.filter(id=student.id)
    # Since you don't have an subject attendance form am assuming 
    # But you sure have to create a form to handle your attendance 
    form = AttentanceForm(request.POST, request.FILES)
    if form.is_valid():
       sub1_att_data = form.save(commit=False)
       sub1_att_data.sub = subject # here you are getting the sub id to save
       sub1_att_data.student = student.id # we are getting the student id to save in the form
       # Basically this to give you an idea on how to make you view work inorder to save data 
      sub1_att_data.save()

    return HttpResponseRedirect('count?subject=Python')