Home > OS >  Django get input radio select in a form
Django get input radio select in a form

Time:07-14

What I want, is a page where per user can be selected if they were either Present, Allowed Absent or Absent. I have managed to create a page where this is possible and can be submitted, see the image I attached. How the code works right now

My problem is, I have no idea how I can get to the data I submitted. I used name={{user.id}} for the name of the radio buttons, but how can I now access this input in my view? I want to be able to do something different if Absent is selected for example, but I don't know how to do that.

Here is my code: baksgewijs/mbaksgewijs_attendance.html

    <form action="{% url 'create_attendance' %}" method="post">
        {% csrf_token %}
        <div >
                <table id = "userTable" >
                        <tbody>
                        {% for user in users %}
                                <tr>
                                        <td> {{ user }}
                                        <td>
                                            <div >
                                                <input  type="radio" name={{user.id}} id="radio" value="Aanwezig">
                                                <label  for="flexRadioDefault1">
                                                    Aanwezig
                                                </label>
                                            </div>
                                            <div >
                                                <input  type="radio" name={{user.id}} id="radio" value="Afwezig">
                                                <label  for="flexRadioDefault2">
                                                    Afwezig
                                                </label>
                                            </div>
                                            <div >
                                                <input  type="radio" name={{user.id}} id="radio" value="Geoorloofd afwezig">
                                                <label  for="flexRadioDefault2">
                                                   Geoorloofd afwezig
                                                </label>
                                            </div>
                                        </td>
                                </tr>
                        {% endfor %}
                        </tbody>
                </table>
        </div>
    </form>
<br>
<button class='btn btn-primary active' type="submit"> Update Peloton </button>

views.py

def baksgewijs_attendance(request):
    users = User.objects.all()
    if request.method == "POST":
        print("I go to view")
        form = CreateAttendance(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            for user in users:
                user_attendance = form.cleaned_data.get(user.id)
                print(user)
            message = messages.success(request, ("De aanwezigheid is opgeslagen"))
            return HttpResponseRedirect(reverse("baksgewijs_index"), {"message" : message})
        else:
            return render(request, 'baksgewijs/mbaksgewijs_attendance.html', {"form": CreateAttendance})
    return render(request, "baksgewijs/mbaksgewijs_attendance.html", {
        "users" : users
    })

forms.py

attendance_choices = (
    ('absent', 'Afwezig'),
    ('allowedabsent', 'Geoorloofd afwezig'),
    ('present', 'Aanwezig'),
)


class CreateAttendance(forms.ModelForm):
    class Meta:
        model = Attendance
        fields = ['user', 'attendance']
        widgets = {
            'attendance': forms.RadioSelect(choices=attendance_choices)
        }

models.py

class Attendance(models.Model):
    attendance = models.CharField(max_length=13)
    user = models.ForeignKey('puma_core.User', on_delete=models.CASCADE)
    baksgewijs = models.ForeignKey(Baksgewijs, on_delete=models.CASCADE)
    def __str__(self):
        return self.attendance

urls.py

urlpatterns = [
    path("", views.baksgewijs_index, name="baksgewijs_index"),
    path("baksgewijs", views.baksgewijs_index, name="baksgewijs_index"),
    path("baksgewijs/<int:baksgewijs_id>", views.display_baksgewijs, name="baksgewijs"),
    path("pelotons", views.peloton_index, name="peloton_index"),
    path("pelotons/<int:peloton_id>", views.display_peloton, name="peloton"),
    path("pelotons/<int:peloton_id>/add", views.update_peloton, name="update_peloton"),
    path("baksgewijs/<int:baksgewijs_id>/edit", views.edit_baksgewijs, name="edit_baksgewijs"),
    path("pelotons/createpeloton", views.createPeloton, name="create_peloton"),
    path("baksgewijs/createbaksgewijs", views.createBaksgewijs, name="create_baksgewijs"),
    path("baksgewijs/attendance", views.baksgewijs_attendance, name="create_attendance"),
    path("pelotons/no_peloton", views.users_without_peloton, name="no_peloton"),
    path("pelotons/<int:peloton_id>/delete", views.delete_peloton, name="delete_peloton"),
    path("baksgewijs/<int:baksgewijs_id>/delete", views.delete_baksgewijs, name="delete_baksgewijs"),
    path("bakgewijs/all", views.all_baksgewijs, name="all_baksgewijs")
]

I have asked a question about this before but I didn't quite figure out how to fix it so I have now tried a different approach. I have the feeling that it doesn't do much with my input now either. In advance, thanks a lot for helping. <3

CodePudding user response:

You just have to add a value to your radio buttons. Then, when retrieving the value of your radio buttons by using request.POST.get('your_input_name') you are getting the value of the radio button that was selected.

You should get something like that in your template :

<div >
    <input  type="radio" name={{user.id}} id="radio" value="Aanwezig">
    <label  for="flexRadioDefault1">
        Aanwezig
    </label>
</div>
<div >
    <input  type="radio" name={{user.id}} id="radio" value="Afwezig">
    <label  for="flexRadioDefault2">
        Afwezig
    </label>
</div>
<div >
    <input  type="radio" name={{user.id}} id="radio" value="Geoorloofd afwezig">
    <label  for="flexRadioDefault2">
       Geoorloofd afwezig
    </label>
</div>

Then, in your views.py :

if form.is_valid():
    form.save()
    for user in users:
        user_attendance = form.cleaned_data.get(user.id)
        # Do something with your user depending on the value of user_attendance
    message = messages.success(request, ("De aanwezigheid is opgeslagen"))
    return HttpResponseRedirect(reverse("baksgewijs_index"), {"message" : message})
  • Related