0

I need some help building a dynamic formset in django. I understand that some javascript is required. I am no javascript expert. But I found some example code and I'm trying to fit it to work on my particular form. When I click the Add button nothing happens. And I'm at a loss on how to move forward. Any suggestions?

Model is for attendee event registration. A customer can register multiple people to attend an event.
Trying to get an add attendee button.

# models.py
class Customer(models.Model):
    event = models.ForeignKey(Event)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30) 
    address1 = models.CharField(max_length=60) 
    address2 = models.CharField(max_length=60, blank=True) 
    city = models.CharField(max_length=30) 
    state = models.CharField(max_length=2) 
    zipcode = models.CharField(max_length=5) 

class Attendee(models.Model):
    event = models.ForeignKey(Event)
    sponsor = models.ForeignKey(Customer)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30) 

# forms.py
CustomerFormset = inlineformset_factory(Customer, Attendee, form=AttendeeForm, exclude=('event'), extra=2)

# templates/register.html
<form action="/enroll/register3/{{ event.id }}/" method="post">{% csrf_token %}
<p> Sponsor information: </p>

<table>
  {{ customer }}
</table>
<hr>
<p> Students Attending: </p>
{% for attendee in attendees.forms %}
<div class="formgroup">
  {{ attendee }}
  {% if attendee.nested %}
  {% for formset in attendee.nested %}
  {{ formset.as_table }}
  {% endfor %} 
  {% endif %} 
</div>
{% endfor %}
 <div>
 Nothing happens when 'add_more' is clicked.  ??
 <input type="button" value="Add Attendee" id="add_more">
 <input type="submit" value="Submit">
</div>
</form>

<script> 
    $('#add_more').click(function() { 
        //cloneMore('div.table:last', 'service'); 
        cloneMore('div.table:last', 'formgroup'); 
    }); 

function cloneMore(selector, type) { 
    console.log('cloneMore'); 
    var newElement = $(selector).clone(true); 
    var total = $('#id_' + type + '-TOTAL_FORMS').val(); 
    newElement.find(':input').each(function() { 
        var name = $(this).attr('name').replace('-' + (total-1) + '-','-' + total + '-'); 
        var id = 'id_' + name; 
        $(this).attr({'name': name, 'id': id}).val('').removeAttr('checked'); 
    }); 
    newElement.find('label').each(function() { 
        var newFor = $(this).attr('for').replace('-' + (total-1) + '-','-' + total + '-'); 
        $(this).attr('for', newFor); 
    }); 
    total++; 
    $('#id_' + type + '-TOTAL_FORMS').val(total); 
    $(selector).after(newElement); 
} 

</script> 
codingJoe
  • 4,713
  • 9
  • 47
  • 61

1 Answers1

1

It looks like you took javascript from this question, but that you did not adapt your HTML structure.

As I can see in your snippet (which also looks like it will produce highly invalid html, ie. tr in divs):

{% for attendee in attendees.forms %}
<div class="formgroup">
  {{ attendee }}
  {% if attendee.nested %}
  {% for formset in attendee.nested %}
  {{ formset.as_table }}
  {% endfor %} 
  {% endif %} 
</div>
{% endfor %}

And his:

{{ serviceFormset.management_form }}
{% for form in serviceFormset.forms %}
    <div class='table'>
    <table class='no_error'>
        {{ form.as_table }}
    </table>
    </div>
{% endfor %}

If you want the same exact javascript piece of code, then you should have the same exact HTML structure. The first thing you're doing in JS is passing div.table:last as selector but you have no div.table in your HTML.

Your best bet is to try to make the javascript yourself. As a webpage developer, javascript will help you a lot.

Community
  • 1
  • 1
jpic
  • 32,891
  • 5
  • 112
  • 113
  • How do I even begin to debug this stuff? When I click on the Add button nothing happens. I tried to add some Firebug stuff like `console.log('click');` But nothing prints when I do that. – codingJoe Jun 20 '12 at 04:22
  • You should place breakpoints in the "Scripts" tab of firebug. http://getfirebug.com/javascript – jpic Jun 21 '12 at 13:59