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 of the formset) to the page.
相关问题
- Carriage Return (ASCII chr 13) is missing from tex
- Django __str__ returned non-string (type NoneType)
- Using :remote => true with hover event
- Is there a way to play audio on a mobile browser w
- Django & Amazon SES SMTP. Cannot send email
Another cloneMore version, which allows for selective sanitization of fields. Use it when you need to prevent several fields from being erased.
Yea I'd also recommend just rendering them out in the html if you have a finite number of entries. (If you don't you'll have to user another method).
You can hide them like this:
Then the js is really simple:
Check out the following solutions to dynamic django forms:
http://code.google.com/p/django-dynamic-formset/
https://github.com/javisantana/django-dinamyc-form/tree/master/frm
They both make use of jQuery and are django-specific. The first seems a bit more polished and offers a download that comes w/demos which are excellent.
One option would be to create a formset with every possible form, but initially set the unrequired forms to hidden - ie,
display: none;
. When it's necessary to display a form, set it's css display toblock
or whatever is appropriate.Without know more details of what your "Ajax" is doing, it's hard to give a more detailed response.
For the coders out there who are hunting resources to understand the above solutions a little better:
Django Dynamic Formsets
After reading the above link, the Django documentation and previous solutions should make a lot more sense.
Django Formset Documentation
As a quick summary of what I was getting confused by: The Management Form contains an overview of the forms within. You must keep that information accurate in order for Django to be aware of the forms you add. (Community, please give me suggestions if some of my wording is off here. Im new to Django.)
I think this is a much better solution.
How would you make a dynamic formset in Django?
Does things clone doesn't: