Dynamically adding a form to a Django formset with Ajax

后端 未结 15 1936
不思量自难忘°
不思量自难忘° 2020-11-22 03:09

I want to automatically add new forms to a Django formset using Ajax, so that when the user clicks an \"add\" button it runs JavaScript that adds a new form (which is part o

15条回答
  •  被撕碎了的回忆
    2020-11-22 03:37

    Simulate and imitate:

    • Create a formset which corresponds to the situation before clicking the "add" button.
    • Load the page, view the source and take a note of all fields.
    • Modify the formset to correspond to the situation after clicking the "add" button (change the number of extra fields).
    • Load the page, view the source and take a note of how the fields changed.
    • Create some JavaScript which modifies the DOM in a suitable way to move it from the before state to the after state.
    • Attach that JavaScript to the "add" button.

    While I do know formsets use special hidden fields and know approximately what the script must do, I don't recall the details off the top of my head. What I described above is what I would do in your situation.

提交回复
热议问题