My main goal is to update data using a modal toggle button within a bootstrap accordion. Each question is retrieved from views.py and displayed within an accordion element. The ideal scenario is for each accordion to have a modal toggle button that, when clicked, opens a modal containing the specific question from that accordion for updating.
The issue I am facing is that in the current implementation, clicking the modal button of the first question correctly displays the data for that question. However, when clicking the modal button of the second accordion (which contains a different question), it still shows the data for the first question. I'm struggling to find a solution to have each modal display the respective data for the corresponding accordion. Below is a snippet of my template file:
{% extends 'base.html' %}
{% block title %}Add Questions{% endblock title %}
{% block body %}
<body>
<label for=""><h4 style="color: indianred">Grade >> {{classname}} >> {{topicname}} >> {{subtopicnames}}</h4></label><br>
<br>
<br>
<br>
</div>
</form>
<div class="panel-group" id="accordion">
{% for x in queryset %} <!-- retrieving all questions from views.py-->
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse{{ forloop.counter }}">
Question
</a>
</h3>
</div>
<div id="collapse{{ forloop.counter }}" class="panel-collapse collapse">
<div class="panel-body">
<!-- <input type="button" onclick="updateques();" value="Edit!!"><br> -->
<br><br>
<div>
{{x.question.id}}
<input type="hidden" id="quesid" name="quesid" value="{{x.question.id}}">
<label>Q- <input type="text" id="ques" name="ques" value="{{x.question.question}}" readonly></label><br><br>
<img src="{{x.question.image}}" alt="" srcset="" width="700"><br><br>
<label>A- <input type="text" id="op1" name="" value="{{x.option.option1}}" readonly><br><br>
<label>B- <input type="text" id="op2" name="" value="{{x.option.option2}}" readonly><br><br>
<label>C- <input type="text" id="op3" name="" value="{{x.option.option3}}" readonly><br><br>
<label>D- <input type="text" id="op4" name="" value="{{x.option.option4}}" readonly><br><br>
<input type="checkbox" name="" value="{{x.answer}}" checked>
<label>{{x.question.difficulty}}</label>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#staticBackdrop">Edit</button>
</div>
</div>
</div>
{{x.question.id}} <!-- checking here if it returns the right question id value of the accordian that is opened and it displays the correct id value but when it comes to modal it takes only the 1st question id and its data-->
<!--Button trigger modal-->
<!--Modal-->
<div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="staticBackdropLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<input type="hidden" id="quesid" name="quesid" value="{{x.question.id}}">
<label>Q- <input type="text" id="ques" name="ques" value="{{x.question.question}}" readonly></label><br><br>
<img src="{{x.question.image}}" alt="" srcset="" width="700"><br><br>
<label>A- <input type="text" id="op1" name="" value="{{x.option.option1}}" readonly><br><br>
<label>B- <input type="text" id="op2" name="" value="{{x.option.option2}}" readonly><br><br>
<label>C- <input type="text" id="op3" name="" value="{{x.option.option3}}" readonly><br><br>
<label>D- <input type="text" id="op4" name="" value="{{x.option.option4}}" readonly><br><br>
<input type="checkbox" name="" value="{{x.answer}}" checked>
<label>{{x.question.difficulty}}</label>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Understood</button>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock body %}
When I tried moving the modal code inside the panel body, it only worked for the first accordion and not for the others. Placing the modal code inside the template for loop resulted in all accordions opening the modal but displaying data only for the first accordion. Any suggestions on how to achieve the desired outcome are greatly appreciated.