jQuery widgets not working for dynamically added formset forms
NickName:DmRvng Ask DateTime:2019-09-22T02:16:05

jQuery widgets not working for dynamically added formset forms

I have a form and a related formset in my view/template. For several fields I use widgets: Select2 and a calendar (date-picker) from django admin app.

I also use dynamic addition of formset forms.

When I render a template for the first time, all widgets work fine. But when I add new formset form, widgets of this new form don't work.

I've read existing questions on this kind of problem: 1, 2, 3, 4, but they either not applicable for me, or not helping (2 and 3), or may be I am just doing something wrong (most likely - I'm new to it).

Much appreciated for any tips.

HTML:

{% extends "docs/base.html" %}
{% load static %}

{% block extrahead %}
    {{ block.super }}
    {{ form_matter.media.css }}

    <link href="/static/django_select2/select2.min.css" type="text/css" media="screen" rel="stylesheet">
{% endblock %}

{% block content %}

    {% if mat_id == None %}
        <form id="MatterForm" action="{% url 'matter_create_url' %}" enctype="multipart/form-data" method="post">
    {% else %}
        <form id="MatterForm" action="{% url 'matter_edit_url' mat_id=mat_id %}" enctype="multipart/form-data" method="post">
    {% endif %}

        {% csrf_token %}
        <fieldset>
            {% for field in form_matter %}
                <div class="form-row">
                    {% if field.errors %}
                        <div>{{ field.errors }}</div>
                    {% endif %}
                    <label class="form-label">{{ field.label_tag }}</label>
                    <div class="form-input">{{ field }}</div>
                </div>
            {% endfor %}
            <h3>RESOLUTIONS</h3>

            <div id="form_set">
                {{ formset_resolutions.management_form }}
                {% for form in formset_resolutions %}
                    <div class="formset-form">
                        {% for hidden in form.hidden_fields %}
                            {{ hidden }}
                        {% endfor %}
                        {% for field in form.visible_fields %}
                            <div class="form-row">
                                {% if field.errors %}
                                    <div>{{ field.errors }}</div>
                                {% endif %}
                                <label class="form-label">{{ field.label_tag }}</label>
                                <div class="form-input">{{ field }}</div>
                            </div>
                        {% endfor %}
                    </div>
                {% endfor %}
            </div>
        </fieldset>
        <br>
        <button class="btn" type="button" id="add_form" style="margin: 0 0 0 25px">ADD</button>
        <button class="btn" type="submit" style="margin: 0 0 0 25px">SAVE</button>
        <br><br><br>
        <div id="empty_form" style="display:none">
            <div class="formset-form">
                {% for field in formset_resolutions.empty_form.visible_fields %}
                    <div class="form-row">
                        {% if field.errors %}
                            <div>{{ field.errors }}</div>
                        {% endif %}
                        <label class="form-label">{{ field.label_tag }}</label>
                        <div class="form-input">{{ field }}</div>
                    </div>
                {% endfor %}
            </div>
        </div>
    </form>

<script type="text/javascript">window.__admin_media_prefix__ = "{% static 'admin/' %}";</script>
<script type="text/javascript" src="/jsi18n/"></script>
<script type="text/javascript" src="/static/js/jquery.js"></script>
<script type="text/javascript" src="/static/django_select2/select2.min.js"></script>
<script>
    $('#add_form').on('click', function() {
        var form_idx = $('#id_resolution_set-TOTAL_FORMS').val();
        $('#form_set').append($('#empty_form').html().replace(/__prefix__/g, form_idx));
        $('#id_resolution_set-TOTAL_FORMS').val(parseInt(form_idx) + 1);
    });
</script>

<script>$('div.form-row:has(label.form-label:has(label[for$="-DELETE"]))').css('background-color', '#ffc7c7')</script>
{{ form_matter.media.js }}
{% endblock content %}

Copyright Notice:Content Author:「DmRvng」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/58042935/jquery-widgets-not-working-for-dynamically-added-formset-forms

More about “jQuery widgets not working for dynamically added formset forms” related questions

jQuery widgets not working for dynamically added formset forms

I have a form and a related formset in my view/template. For several fields I use widgets: Select2 and a calendar (date-picker) from django admin app. I also use dynamic addition of formset forms....

Show Detail

ID of dynamically added formset forms in django templates

This may be a silly question, but: When rendering forms in a formset in a django template, one has to render the ID's as well. This I get. When I dynamically create a new form in the formset with...

Show Detail

Dynamically add form to formset in Django and submit with AJAX

I have read a lot of answers relating to how to dynamically add forms to an model formset in Django and can successfully implement that. However, I would now like to submit the formset with AJAX. T...

Show Detail

Dynamically added Django FormSet data not being posted

I am modifying a FormSet using JavaScript/jQuery by dynamically adding a form to a Django FormSet. For example, I start with one form asking about a User's education. The User can then press an add

Show Detail

Django inline formset - allow blank dynamically generated forms

I'm using an inline formset with some javascript to add more forms with a button press. One of the views keeps trying to validate blank, dynamically added forms and is triggering "this field is req...

Show Detail

Add forms to a Django formset dynamically using htmx?

There are several writeups on how to use javascript to add forms dynamically to a Django formset. For example this or this. I just learned about htmx. How can I add forms dynamically to a Django f...

Show Detail

Dynamically added forms not saving to inlineformset_factory Django

I have been following a variety of tutorials for a while and trying to understand/put them all together to achieve the ability for my user to dynamically add instances to a child within an

Show Detail

Django - multiple inline forms validate 1 formset using another formset data

I have a main Model: MainModel -> MainModelForm that's used in a view I also have 2 inline forms that have the MainModel as primary key InlineModel1 => InlineModel1Form (formset =

Show Detail

creating a django form in formset dynamically like inline forms in django admin with jquery

I have a two models Publisher and Book like below models.py class Publisher(models.Model): name = models.CharField(max_length=255) class Book(models.model): name = models.CharField(max_l...

Show Detail

Dynamically adding forms in formset not saving / Management form manual update?

I've been following this tutorial, and the form duplication/removal part works great however, the only the first form is saved. From the formset's data, I know the values exist as well as that form-

Show Detail