Button for adding an instance of formset in Django
I want to create a button for dynamically adding and removing an instance of formset from the displayed page. It is my first experience with formset in Django and I do not know any JavaScript, so I would be grateful for any help. So, lines for the formset in my view look like this:
strategy_settings = formset_factory(StrategySettings, extra=1)
strategy_form = strategy_settings(request.POST)
I believe it is a matter of changing the value of an "extra" attribute, but I do not know how to achieve it in JavaScript.
here is my template:
{% extends "mainapp/base.html" %}
{% block content %}
<form method="post" id="strategy-form">
{% csrf_token %}
{{ simulation_form.as_p }}
{% for form in strategy_form %}
{{ form.as_p }}
{% endfor %}
<button type="button">Add Strategy</button>
<button type="button">Remove Strategy</button>
<button type="submit" name="save">Submit</button>
</form>
{% endblock %}
Thanks to anyone in advance.
The extra
value determines how many additional, empty forms are displayed initially.
As for the dynamic stuff, since you don't know any javascript (yet), I suggest you look into a 3rd party package that does what you want. For example here: https://djangopackages.org/search/?q=formset.