0

I am working on an attendance management system. I want to modify attendance of students.

class Subject(models.Model):
    subject_name = models.CharField(max_length=20)
    #attendance = models.ForeignKey(Attendance, on_delete = models.DO_NOTHING)
    attendance = models.IntegerField(default=0)
    def __str__(self):
        return self.subject_name


class Section(models.Model):
    section_name = models.CharField(max_length=20)
    subject = models.ManyToManyField(Subject)
    def __str__(self):
        return self.section_name


class Student(models.Model):
    rollno = models.IntegerField()
    name = models.CharField(max_length=20)
    section = models.ForeignKey(Section, on_delete = models.DO_NOTHING, default=0)
    def __str__(self):
        return str(self.rollno) + self.name 

Here is my template. (Student.html)

{% for i in data %}  
                <tr>
                    <td>{{ i.rollno }}</td>
                    <td>{{ i.name }}</td>
                    <td> <button class='btn btn-danger' id='{{i.rollno}}' on click = "{{ i.section.subject.get(subject_name='java').attendance)|add:1 }}"> 
                    </td>
                </tr>
{% endfor %}

I am getting error in using .get() method in template. I want to add (+1) attendance on a button click.

2
  • But that is not at all a thing you can do in the template, even leaving aside the syntax issues. Things like this must be done in the view. Commented Jul 7, 2018 at 8:10
  • Thanks for your reply. Please help me. What should pass to the view as argument and how? I am new to django. @DanielRoseman Commented Jul 7, 2018 at 8:19

1 Answer 1

1

I strongly suggest to go through Django Tutorial. You will learn Django MVC concepts and able to achieve what you asked easily. The below code will help you get started.

views.py

def increment_attendance(request, subject_id):
  """Increment Attendance for a Subject"""

    subject = Subject.objects.get(id=subject_id)
    # check if record exists
    if not subject:
        raise Http404("Invalid subject_id")

    # can also use only get_object_or_404(Subject, pk=subject_id)

    # increment attendance
    subject.attendance += 1
    # save / commit to database
    subject.save()

    # redirec to 'some' page or previous page?
    return redirect('top')

add this path to your urls.py

  path('subject/<int:day>/increment_attendance', views.increment_attendance, name='increment_attendance')

template

  <a class="btn btn-danger" id="{{i.rollno}}" href="{% url 'increment_attendance' subject_id=subject_id" %}"></a>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Zekoi. Will you please comment on my models.py. This is my first project and I am not sure about the database I've made.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.