Monday, March 30, 2015

Re: Help with Dynamically Adding Forms to Formset!!

Hi Stephanie,
I've had success with the following jQuery library:


You can administer the formsets normally in your view and forms files.  Reference here:

Here is a code snippet of a template that I've written for my app.  You can modify accordingly to try to back into a solution:
...
...
<script src="{% static 'jquery.formset.js' %}"></script>
<script type="text/javascript">
    $(function() {
        $('.itinerary-bootstrap .formset').formset({
            prefix: '{{ formset.prefix }}',
            addText: 'Add Another Leg',
            deleteText: 'Remove Leg',
        })
    })
</script>
...
...
    <div id="itinerary-bootstrap" class="itinerary-bootstrap">
        {% for form in formset.forms %}
            <div class="formset">
                <div id="{{ form.prefix }}-row">
                    <div class="col-md-2">
                        <div class="row">
                            {{ form.date }}
                        </div>
                        <div class="row">
                            {{ form.ETD }}
                        </div>
                    </div>
                    <div class="col-md-4 col-md-offset-1">
                        <div class="row">
                            {{ form.origin }}
                        </div>
                        <div class="row">
                            {{ form.destination }}
                        </div>
                    </div>
                </div>
            </div>
        {% endfor %}
        {{ formset.management_form }}
        <br><br>
    </div>
....
....

On Mon, Mar 30, 2015 at 5:18 PM, Carl Meyer <carl@oddbird.net> wrote:
Hi Stephanie,

On 03/30/2015 02:37 PM, Stephanie Socias wrote:
> I'm having trouble creating a formset where I can dynamically add and
> delete items. I've tried many different approaches and can't get
> anything to work. I'm new to Django and have been at this for days. Can
> someone please help me!? I can send my code, post it, or do a screen
> share. Please let me know!! Any help would be greatly appreciated.

I'm afraid I don't have time in my day for a screen-share, but I'd be
happy to try to help, if you can link to a gist or paste showing your
current best effort, and explaining how it is not working.

FWIW, I've usually used this library for the JavaScript side of things:
https://github.com/jgerigmeyer/jquery-django-superformset

If you'd find real-time assistance more helpful, you could visit the
#django channel on FreeNode IRC; I and others are available to help out
there.

This is a challenging task you've tackled, even for experienced Django
developers, so it's not surprising that it's taking some time if you're
new to Django.

Carl

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5519BD9B.9080105%40oddbird.net.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAP7uED%2B_2_CpVgnYKUETXYNXH1m5BdDcH7NFRHWvBXjUn-iu7Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment