I would like to retrieve column data from a table after checking the desired checkboxes. Could you please help?
Below I would like to present my code below. In checkbox.html and views.py I don’t know exactly how to retrieve data from the table after seting the checkboxes.
startapp/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('checkbox/', views.checkbox, name='checkbox'),
path('', views.home1, name='home1'),
]
startapp/views.py
def home1(request):
patients = Patients.objects.all()
context = {
'patients': patients
}
if request.method == 'post':
s = request.POST.getlist('s')
print(s)
return render(request, 'home1.html', context)
def checkbox(request):
patients = Patients.objects.all()
context = {
'patients': patients
}
#Here I have to put any code for connection with checkbox.html
return render(request, 'home1.html', context)
startproj/urls.py
from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('startapp.urls')),
]
home1.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
Checkboxes will take the information from the table columns!<form action="" method="post">
{% csrf_token %}
<input type="checkbox" name="s" value="name">Name
<br><br>
<input type="checkbox" name="s" value="years">Years
<br><br>
<input type="checkbox" name="s" value="apress">Arterial Pressure
{% csrf_token %}
<button type="submit">submit</button>
</form>
<div class="container">
<div class="row mt-4">
<div class="col">
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">PatientID</th>
<th scope="col">Name</th>
<th scope="col">Years</th>
<th scope="col">Probe Date</th>
<th scope="col">Birth Date</th>
<th scope="col">Arterial Pressure</th>
</tr>
</thead>
<tbody>
{% for patient in patients %}
<tr>
<td>{{ patient.patient_id }}</td>
<td>{{ patient.name }}</td>
<td>{{ patient.years }}</td>
<td>{{ patient.probe_date }}</td>
<td>{{ patient.birth_date }}</td>
<td>{{ patient.arterial_pressure }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
<p>More data here! The page is under construction!</p>
<a href="{% url 'checkbox' %}">Checkboxes result</a>
</body>
</html>
checkbox.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="" method="post">
#if checkbox[‘name’].is_checked:
#take date from the column[name]
#if checkbox[‘years’].is_checked:
#take date from the column[years]
#if checkbox[‘name’].is_checked and :checkbox[‘years’].is_checked
#take date from the column[name] and column[years]
</form>
</body>
</html>