Django - Default on multiple radio buttons

I am making an absence control where you select the presence/absence per user through a radio button. This absence control is already working. But right now, when you save the absences and re-open the page, all the radio buttons are unselected so it is not possible to see what was filled in. How can I fix this so you can see in which way the radio buttons where selected?

The absence control is saved through this view:

def baksgewijs_attendance(request, baksgewijs_id, peloton_id):
    peloton = Peloton.objects.get(id=peloton_id)
    users = peloton.users.all()
    baksgewijs = Baksgewijs.objects.get(id=baksgewijs_id)
    random_list = list()
    for usertje in users:
        if Absences.objects.filter(date=baksgewijs.date, user=usertje).exists():
            random_list.append(getattr(Absences.objects.get(date=baksgewijs.date, user=usertje), "explanation"))
        else:
            random_list.append("")
    zipped_list = zip(random_list, users)
    if request.method == "POST":
        print(request.POST)
        id_list = request.POST.getlist('id')
        for id in id_list:
            if request.POST.getlist(id):
                attendance = Attendance()
                attendance.user = User.objects.get(pk=id)
                attendance.attendance = request.POST.getlist(id)[0]
                attendance.baksgewijs = Baksgewijs.objects.get(id=baksgewijs_id)
                attendance.save()
        message = messages.success(request, ("De aanwezigheid is opgeslagen"))
        return HttpResponseRedirect(reverse("baksgewijs_index"), {"message" : message})
    return render(request, "baksgewijs/mbaksgewijs_attendance.html", {
        "users" : users,
        "peloton" : peloton,
        "baksgewijs" : baksgewijs,
        "zipped_list" : zipped_list
    })

This is baksgewijs/mbaksgewijs_attendance.html:

  <form action="{% url 'create_attendance' baksgewijs.id peloton.id %}" method="post">
        {% csrf_token %}
        <div class="table-responsive fixed-length">
                <table id = "userTable" class="table table-striped table-hover table-sm table-bordered">
                        <tbody>
                        {% for reason, user in zipped_list %}
                                <tr>
                                        <td> {{ user }} <br> {{ reason }} </td>
                                        <td>
                                            <input type='hidden' value='{{user.id}}' name='id'>
                                            <div class="form-check">
                                                <input class="form-check-input" type="radio" name={{user.id}} id="radios" value="Aanwezig">
                                                <label class="form-check-label" for="flexRadioDefault1">
                                                    Aanwezig
                                                </label>
                                            </div>
                                            <div class="form-check">
                                                <input class="form-check-input" type="radio" name={{user.id}} id="radios" value="Afwezig">
                                                <label class="form-check-label" for="flexRadioDefault2">
                                                    Afwezig
                                                </label>
                                            </div>
                                            <div class="form-check">
                                                <input class="form-check-input" type="radio" name={{user.id}} id="radios" value="Geoorloofd afwezig">
                                                <label class="form-check-label" for="flexRadioDefault2">
                                                   Geoorloofd afwezig
                                                </label>
                                            </div>
                                        </td>
                                </tr>
                        {% empty %}
                            Er zijn geen gebruikers in dit peloton.
                        {% endfor %}
                        </tbody>
                </table>

The three radio buttons are present, absent and allowed absent.

the url "create_attendance" just looks like this:

path("attendance/<int:baksgewijs_id>/<int:peloton_id>", views.baksgewijs_attendance, name="create_attendance")

Attendance Model:

class Attendance(models.Model):
    attendance = models.CharField(max_length=256)
    user = models.ForeignKey('puma_core.User', on_delete=models.CASCADE)
    baksgewijs = models.ForeignKey(Baksgewijs, on_delete=models.CASCADE)
    def __str__(self):
        return f'{self.user} is {self.attendance} bij {self.baksgewijs}'

Thanks in advance, could really use some help :)

Back to Top