Hello, I am creating a website that a patient can manually insert their data and save into a database. Then, the data can be accessed and be modified like an “edit view”. I am using django.
I need help on displaying the saved data on the html.
I was able to figure out how to display the original data of the patient on the input that are text or integers. However, I can’t find a way to display the original data on the checkbox.
First, I will search the patient’s name
After searching the patient’s name
However, I would like the checkbox to be checked based on the data.
Could you help me on coding for the html?
users = patient’s data
form = form field to get an input
Any help will be greatly appreciated. Thank you in advance.
**** Need to modify {form.q1}} and {{form.q2}} to be checked or unchecked based on user.q1 and user.q2****
***user.q1 is MultipleChoiceField ***
***user.q2 is a true/false data ***
modify_patient.html
<h2>Patient Details</h2>
<br>
<form method="POST">
{% csrf_token %}
{% for user in users%}
<div class="container">
<form method="POST">
{% csrf_token %}
<table>
<tr>
<th class="one"> Name: </th>
<th class = "two">{{ user.patient_name}}</th>
<th class="three">Birthdate </th>
<th class = "four">{{ user.birthdate}}</th>
</tr>
<tr>
<th class="one">Age</th>
<th class = "two"><input type="integer" name = "age" id="age" value = {{user.age}}> </th>
<th class = "three">Emotion </th>
<th class = "four">{{ form.q1 }}</th>
</tr>
<tr>
<th class="one"> Do you sleep well? </th>
<th class = "two">{{ form.q2}}</th>
</tr>
</table>
{% endfor %}
<BR>
<button style = "border:0px; background-color:#4285F4;
height:40px; width:80px;margin-left:35%;" type = "submit"
value = "next" >
<strong>Next</strong>
</button>
<br><br>
</div>
</form>
{% endblock content%}
model.py
class Patient(models.Model):
q1_choices = (('a1','Happy'),
('a2','Sad'),
('a3','Depressed'),
('a4', 'Tired'))
#general info
patient_name = models.CharField(max_length=20, )
birthdate = models.DateField()
age = models.PositiveSmallIntegerField(max_length=20)
q1 = MultiSelectField(blank=True, choices = q1_choices)
q2 = models.BooleanField( default =False, blank=True)
def __str__(self):
return self.patient_name
forms.py
class PatientModelForm(ModelForm):
q1_choices = (('a1','Happy'),
('a2','Sad'),
('a3','Depressed'),
('a4', 'Tired'))
birthdate = forms.DateField(widget=SelectDateWidget(years=range(datetime.date.today().year-100,datetime.date.today().year+10)))
q1 = forms.MultipleChoiceField(choices = q1_choices, required=False, widget=forms.CheckboxSelectMultiple())
class Meta:
model = Patient
fields = '__all__'
widgets = { 'q2': forms.CheckboxInput(attrs={'style':'width:30px;height:30px;'}), }
views.py
def modifyIntro(request):
if request.method == 'POST':
name = request.POST['name']
bdate = request.POST['birthdate']
if Patient.objects.filter(patient_name = name, birthdate = bdate):
print("hi")
forms.change_id = Patient.objects.get(patient_name = name, birthdate = bdate).id
return redirect('webapp-modifyInfo')
else:
messages.warning(request, f'No patient exist. Please try again.')
return render(request, 'webapp/about_patient.html')
def modifyInfo(request):
print("h2")
q1_choices = (('a1','Happy'),
('a2','Sad'),
('a3','Depressed'),
('a4', 'Tired'))
if request.method == 'POST':
form = PatientModelForm(request.POST)
print(form.errors)
if form.is_valid():
o =form.save()
# s =Patient.objects.filter(id = forms.change_id).update(sleep_id = o.id)
return render(request, 'webapp/display_patient.html',{'users': Patient.objects.filter(id= forms.change_id)})
else:
form_modify =PatientModelForm
return render(request, 'webapp/modify_patient.html', { 'q1':q1_choices, 'users': Patient.objects.filter(id=forms.change_id), 'form': form_modify} )