A good option is using the Vue.js framework to build components and fill your form dynamically.
I like very much Vue.js and I built an app to solve your problem.
I'll go step by step explaining what I did.
1. Let's simulate your objects with some data
views.js
from django.shortcuts import render
def home(request):
users = [dict(id=1, first_name='Megan', last_name='Fox'),
dict(id=2, first_name='Chester', last_name='Bennington')]
if request.POST:
for user in users:
if user['id'] == int(request.POST.get('id')):
user['first_name'] = request.POST.get('firstName')
user['last_name'] = request.POST.get('lastName')
return render(request, 'index.html', {'users': users})
2. Import an EventBus built in Vue
We are going to need that event bus to show the modal. The event bus is the component which will "transport" events from one component to another. So we can listen and emit events globally.
vue-bus.js
const EventBus = new Vue();
Object.defineProperties(Vue.prototype, {
$bus: {
get: function () {
return EventBus;
}
}
});
3. Build a row component with Vue to show your data.
I changed your data to firstName and lastName to explain better.
This component renders your row with data and set up the edit button to emit the event showModal passing the data from the row.
components/row.js
Vue.component('row', {
template: `
<tr>
<td>{{ firstName }}</td>
<td>{{ lastName }}</td>
<td>{{ id }}</td>
<td><button @click="showModal">Edit</button></td>
</tr>
`,
props: ['firstName', 'lastName', 'id'],
methods: {
showModal: function() {
this.$bus.$emit('showModal', this.firstName, this.lastName, this.id);
}
}
});
4. Build a modal component with Bootstrap
This component uses a x-template on index.html to render its contents. On this component all you need is a listener to the showModal event, so when the modal opens, it will be populated with the row content.
components/modal.js
Vue.component('modal', {
template: '#modal-template',
data: function () {
return {
firstName: '',
lastName: '',
id: 0
}
},
mounted: function () {
this.$bus.$on('showModal', function (firstName, lastName, id) {
this.firstName = firstName;
this.lastName = lastName;
this.id = id;
}.bind(this));
}
});
5. Build your Vue.js app
If you take a look at Vue.js docs you will see how it works. Basically, it will mount your application in the element with id #app.
Here I'm just listening to the showModal event, so when this event is emitted, the modal shows up.
app.js
new Vue({
el: '#app',
data: {
showModal: false
},
mounted: function () {
this.$bus.$on('showModal', function () {
this.showModal = true;
}.bind(this));
}
});
6. Some CSS to make it pretty
css/styles.css
table {
margin: 20px;
}
td {
padding: 10px;
border: 1px solid #ccc;
}
.modal-mask {
position: fixed;
z-index: 9998;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, .5);
display: table;
transition: opacity .3s ease;
}
.modal-container {
transition: all .3s ease;
}
.modal-enter {
opacity: 0;
}
.modal-leave-active {
opacity: 0;
}
7. Finally, the HTML
So, what I'm doing here is iterating over the users lists populating the rows with the user data.
The modal template is inside a x-template as I said before. You need to do some changes on Bootstrap modal to make this work like adding the class show and changing the data-dismiss attribute to close the modal.
The data is populated from the listener on components/modal.js. The bind is made on :value. See the v-bind documentation on Vue website to understand better how it works.
index.html
{% load static %}
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
</head>
<body>
<div id="app">
<table>
{% for user in users %}
<tr is="row" first-name="{{ user.first_name }}" last-name="{{ user.last_name }}" :id="{{ user.id }}"></tr>
{% endfor %}
</table>
<modal v-show="showModal" @close="showModal = false"></modal>
</div>
<script src="http://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="//unpkg.com/vue"></script>
<script type="text/x-template" id="modal-template">
<transition name="modal">
<div class="modal-mask">
<div class="modal-container">
<div id="myModal" class="modal show" role="dialog">
<div class="modal-dialog">
<form method="post" action=".">
{% csrf_token %}
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" @click="$emit('close')">×</button>
<h4 class="modal-title">Edit User</h4>
</div>
<div class="modal-body">
<label for="name">First Name:</label>
<input type="text" class="form-control" name="firstName" :value="firstName" />
<label for="lastName">Last Name:</label>
<input type="text" class="form-control" name="lastName" :value="lastName" />
<input type="hidden" name="id" :value="id" />
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Save</button>
<button type="button" class="btn btn-default" @click="$emit('close')">Close</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</transition>
</script>
<script type="text/javascript" src="{% static 'vue-bus.js' %}"></script>
<script type="text/javascript" src="{% static 'components/row.js' %}"></script>
<script type="text/javascript" src="{% static 'components/modal.js' %}"></script>
<script type="text/javascript" src="{% static 'app.js' %}"></script>
</body>
</html>
The Result

When we click on Edit:

Then we edit the data:

And save:
