Moving javascript code and form outside a for loop

This is more a JavaScript issue than a Django issue - you may get more & better help in a JavaScript-related forum.

In general, you can add the data elements you’re going to need as custom attributes to the individual elements being rendered, then access them through the this reference for the event handler. This allows you to render one instance of the code and modal box, and update it from your JavaScript on an on-demand basis.