How to create dynamic form field? Django 2.0

523 views
Skip to first unread message

Django Lover

unread,
Jul 19, 2018, 1:29:51 PM7/19/18
to Django users

C. Kirby

unread,
Jul 19, 2018, 1:56:29 PM7/19/18
to Django users
https://docs.djangoproject.com/en/2.0/topics/forms/formsets/ to allow for multiple instances of the same form type. Then you will need some javascript to handle showing additional instances of the form when the button is clicked

Django Lover

unread,
Jul 19, 2018, 2:18:07 PM7/19/18
to Django users
This is hard for me understand can you please give some code. I am new in Django. PLEASE HELP ME

mottaz hejaze

unread,
Jul 19, 2018, 3:53:13 PM7/19/18
to django...@googlegroups.com
this is not a django stuff , this is frontend .. you can use jquery or angularjs ..to hundle click event on the button and create the two fields ...

this website cant be done with django templates.. you need djangorest framework with frontend javascript library

the django template way , you create a button that links to a separate form .. the user enters tax name and value ,then submit , if he want to add more , he will repeat the process

--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/734e4c1c-ec9c-4627-93c8-5d2e692c9447%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sunil Kothiyal

unread,
Jul 19, 2018, 5:13:39 PM7/19/18
to django...@googlegroups.com
Thank you for responding. Please can you tell me how i can save it  if i will generate multiple form field using frontend javascript.

You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/GOBFMJqL20Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Melvyn Sopacua

unread,
Jul 20, 2018, 5:45:06 AM7/20/18
to django...@googlegroups.com
On donderdag 19 juli 2018 16:18:07 CEST Django Lover wrote:
> This is hard for me understand can you please give some code. I am new in
> Django. PLEASE HELP ME

If you're new to Django don't try to do this and learn more about Django first.
You don't have the knowledge to break down your problem into the right
components, which for a Django developer up to the task would be very simple
(as shown by the replies).
Walk before your run.
--
Melvyn Sopacua

Django Lover

unread,
Jul 20, 2018, 5:51:13 AM7/20/18
to Django users
@Melvyn Sopacua Can you please give me simple solution of this problem?

alex eckert

unread,
Jul 20, 2018, 2:43:29 PM7/20/18
to Django users
There is no simple solution to this problem. I'm going to try and break this explanation down into simpler parts for you, but please remember this is pretty hard stuff if you're not familiar with Django. I would second @Melvyn's advice, but if you really need to get this done you need to be patient and read.

The general idea is that you create a form, and then you make a formset which is a form of forms. The formset holds multiple forms at the same time, so in your case, you would need a form for a 'Tax' such as 'TaxForm' and then you also would need a formset to hold all the TaxForms such as 'TaxFormset'.

Django formsets (mentioned above) are only the backend part of how this solution comes together. Spend some time reading the documentation that @C Kirby linked. They explain that a formset contains two parts: the management forms and x number of forms. The management form will say to Django "okay we're entering three forms, which is one more than we used to have." This management form allows Django to match up each of the forms to their respective objects. All these parts get entered in in variables of the POST request.

Now, the problem is that Django only helps you on the backend here. Django expects the management form to be filled out correctly, and the data in the formsets to be lined up. To achieve this, you have to do work on the frontend to create things like an 'add' button or a 'remove' button. The javascript on the front end is responsible for turning a button press (something like 'click to add tax' into "tax-INITIAL-FORMS=1" and "tax-TOTAL-FORMS=2."

Personally, I've used this JQuery script: https://github.com/elo80ka/django-dynamic-formset which has allowed me to have an 'add' and 'remove' buttons similar to what you have shown. It will handle the heavy lifting of making this translation of button presses to management form data.

Hope this helps.
Reply all
Reply to author
Forward
0 new messages