I'm trying to build a html template as part of a Django project. It would contain dynamic accordions (number of such accordions determined by the value passed to it from views.py) and the content within each would also be dynamically passed from views.py (I haven't even built this part yet).
I've tried Googling but always end up with one of two scenarios. One where all the accordion buttons open/close only the first accordion. Two, none of the accordion buttons work. Ideally, all accordion buttons should work and they each need to open/close its own content. I'm guessing the issue lies in dynamically referring to the div that contains the content. But I'm unable to get a fix.
Please help!
Code:
<div class="container">
<h2>Mock Tests</h2>
<p>Click the category you wish to view.</p>
@{int i=0;}
{% for s in sections %}
<div class="panel-group" id="accordion_@i">
<div class="panel panel-default" id="panel_@i">
<div class="panel-heading">
<h4 class="panel-title">
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#collapseone_@i" href="#collapseOne_@i">{{ s.s_name }}</button>
</h4>
</div>
<div id="collapseOne_@i" class="panel-collapse collapse in">
<div class="panel-body">{{ s.s_name }}</div>
</div>
</div>
</div>
i++;
{% endfor %}
</div>
Modified code:
{% block body %}
<div class="container">
<h2>Mock Tests</h2>
<p>Click the category you wish to view.</p>
<div class="panel-group" id="accordion">
{% for s in sections %}
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<button type="button" class="btn btn-info" data-parent="#accordion" data-toggle="collapse" data-target="accordion_{{ forloop.counter }}" href="accordion_{{ forloop.counter }}">{{ s.s_name }}</button>
</h4>
</div>
<div id="accordion_{{ forloop.counter }}" class="panel-collapse collapse in">
<div class="panel-body">{{ s.s_name }}</div>
</div>
</div>
{% endfor %}
</div>
</div>