Search code examples
pythondjangotwitter-bootstrapformsbootstrap-modal

Bootstrap modal not updating modal content


I am trying to create a table of models with a button next to them which opens a modal and has the same model row in form view. The table is being populated correctly, but the n number of bootstrap modals being created only hold the first iterable model value. Is it because bootstrap loads the content of the modals only once when the page is rendered ? What do I do to solve the problem ? Should I run a function to update the modal content according to the model data it has ??

Feel free to ask any more clarifications.

{% extends 'base.html' %}

{% load static %}

{% block content %}
    <table>
        {% for item in data %}

            <tr>
                <th>From</th>
                <th>To</th>
                <th>Weight</th>
                <th>Length</th>
                <th>Type</th>
                <th>Material Type</th>
                <th>Number of Trucks</th>
                <th>Loading Time</th>
            </tr>
            <tr>
                <td>{{ item.From }}</td>
                <td>{{ item.To }}</td>
                <td>{{ item.Weight }}</td>
                <td>{{ item.Length }}</td>
                <td>{{ item.Type }}</td>
                <td>{{ item.MaterialType }}</td>
                <td>{{ item.Numberoftrucks }}</td>
                <td>{{ item.Loadingtime }}</td>
                <td>
                    <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Bid
                        now! for id {{ item.id }} </button>
                </td>
                {#        {% endfor %}#}


                <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                        aria-hidden="true">&times;</span></button>
                                <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                            </div>
                            <div class="modal-body">

                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.To }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.From }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.Weight }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.Length }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.Type }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.MaterialType }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.Numberoftrucks }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.Loadingtime }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here...">Bid
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                <button type="button" class="btn btn-primary">Save changes</button>
                            </div>

                        </div>
                    </div>
                </div>

            </tr>
        {% endfor %}
    </table>

{% endblock %}

Solution

  • you can fix it by add model.pk to the modal id

    in the button

    <button type="button" class="btn btn-primary btn-lg" data-toggle="modal"
         data-target="#myModal{{ item.id }}">
        Bid now! for id {{ item.id }}
    </button>
    

    and in the

     <div class="modal fade"
          id="myModal{{ item.id }}"
          tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    

    other solution is create js function to load new data to each time as you activate the modal.