thank your help:
i’m trying a lot in the last hours but I cannot reach results
basically I would like that if user select an option from dropdown, automatically the page must create list of field with the parameter inside the EsameObiettivo.score json structure:
Jsonfield is like
{[“question1”:“question1”, “answer”:“”],[“question2”:“question2”, “answer2”:“”],…}
so the field are
text with question1 - input text field for answer1
text with question1 - input text field for answer2
I’m trying with this view:
@login_required(login_url='adminlogin')
@user_passes_test(is_admin_or_doctor)
def add_patient_visita_view(request,pk):
print("----------------",pk, request)
patient=models.Patient.objects.get(id=pk)
patientForm=forms.PatientForm(instance=patient)
scoreList = [{'id': i.id, 'title':i.get_title} for i in models.EsameObiettivo.objects.all()]
if request.method=='GET':
if request.GET.get('scoreList_id'):
scoreQuestion = models.EsameObiettivo.objects.get(id=request.GET.get('scoreList_id'))
mydict={'scoreQuestion': scoreQuestion.score}
else:
visita=models.Visita.objects.all()
visita=models.Visita.objects.all().filter(patient_id=patient.get_patient_id).latest('created')
if visita:
visitaForm=forms.PatientVisitaForm(instance=visita)
else:
visitaForm=forms.PatientVisitaForm()
scoreQuestion = {}
mydict={'patientForm':patientForm,'visitaForm':visitaForm,'scoreList':scoreList,'scoreQuestion': scoreQuestion}
elif request.method=='POST':
print("REQUEST POST",request.POST)
visitaForm=forms.PatientVisitaForm(request.POST)
if visitaForm.is_valid():
visitaNew = visitaForm.save(commit=False)
visitaNew.id = None
visitaNew.patient_id = pk
visitaNew.doctor_id = 2
visitaNew.save()
mydict={'patientForm':patientForm,'visitaForm':visitaForm,'scoreList':scoreList}
else:
scoreQuestion = {} #1
mydict={'patientForm':patientForm,'visitaForm':visitaForm,'scoreList':scoreList,'scoreQuestion': scoreQuestion}
#return HttpResponseRedirect(reverse('update-patient', kwargs={"pk": pk}))
return render(request,'hospital/admin2patient/admin_add_patient_visita.html',context=mydict)
and ajax function:
<script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
$("#scoreList").change(function () {
var pageURL = $(location).attr("href");
const scoreListId = $(this).val(); // get the selected subject ID from the HTML dropdown list
$.ajax({ // initialize an AJAX request
type: "GET",
url: pageURL,
data: {
'scoreList_id': scoreListId,
//'csrfmiddlewaretoken': '{{ csrf_token }}',
},
});
});
});
</script>
but never return new value. Initially I trying imposing scoreQuestion to a simple number instead of json structure that would change the number in
{% if scoreQuestion %}
<p id="idtemp">{{ scoreQuestion }}</p>
{% else %}
<p>none</p>
{% endif %}