0

I am trying to create a Bootstrap accordian for a list of items in a Django database. When rendered, currently my code will only collapse and expand on the first item in the for loop. For other items, when I click on these the first item expands/collapses, not the item being clicked on. Looking for a solution please.

<div class="panel-group">
            <div class="panel panel-default">
                {% for issue in issues %}
                <div class="panel-heading">
                    <div class="panel-title">
                        <a data-toggle="collapse" data-target="#collapse1"><h3><i class="fas fa-chevron-down"></i> {{ issue.title }}</h3></a>
                    </div>
                </div>
                <div id="collapse1" class="panel-collapse collapse">
                    <p><strong>Type: </strong>{{ issue.type }}</p>
                    <p class="issue-description"><strong>Description: </strong>{{ issue.description }}</p>
                    <p><strong>Requested By: </strong>{{ issue.requested_by }}</p>
                    <p><strong>Date Created: </strong>{{ issue.date_created }}</p>
                    <p><strong>Status: </strong>{{ issue.status }}</p>
                    <p><strong>Date Completed: </strong>{{ issue.completed_date }}</p>
                    <p><strong>Upvotes: </strong>{{ issue.upvotes }}</p>
                </div>
                {% endfor %}
            </div>
        </div>
2
  • 1
    The question not about Python, you should remove the Python tag Commented Jul 12, 2019 at 22:35
  • You loop over the issues, but all divs have the same id "collapse1". You should make the id of the div (and the linked data-target="#collapse1") dynamic by using for example the issue type or issue status or perhaps some other unique property of issue. Consider this snippet: <div id="collapse-{{issue.id}}" class="..."> Commented Jul 12, 2019 at 22:51

1 Answer 1

2

Give your div unique id like <div id="collapse-{{ issue.pk }}" class="panel-collapse collapse"> and also reference it in data-target: data-target="#collapse-{{ issue.pk }}"

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.