Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show and hide dynamically fields in Django form

I have a form in Django:

views.py:

class SearchForm(forms.Form):
    type = forms.ChoiceField(choices = ...)
    list1 = forms.ModelMultipleChoiceField(...)
    list2 = forms.ModelMultipleChoiceField(...)

home.htm:

<td valign='top'>{{ form.type }}</td>
<td valign='top'>{{ form.list1 }}</td>
<td valign='top'>{{ form.list2 }}</td>
<td valign='top'><input type="submit" value="Find" /></td>

I want the list1 element to be shown and the list2 to be hide if type is 1 and vice versa in case type is 2. I want them to be hide and shown dynamically without reloading the page or any interaction with the server.

I believe Javascript could be useful here, but I don't know it.

like image 678
Nikolay Kovalenko Avatar asked Feb 28 '13 13:02

Nikolay Kovalenko


People also ask

How to create dynamic forms in Django?

Dynamic forms in Django done by generating class objects on the fly with customized fields. A simple example is given how dynamic forms can help saving code and simplify models.py by taking advantage of type native method in python.

How to find hidden fields in a Django form?

Inspect the page and go to the Elements tab in the developer tools - you should see the following: Django's formsets include a lot of hidden fields. The { { formset.management_form }} renders them in the template.

How to create a single database field in Django?

We can create just one database field, to which we would commit all the parameters and values, irrespectively of their number and names. This can simply be done serializing all entries in a e.g. JSON form. In order to make it work, however, we will need dynamic forms . In this post, we will see how we can do it in Django.

What is formset in Django?

A formset is a collection of Django Forms. Each formset has a management form which is used to manage the collection of forms contained in it. Formset stores data like the total number of forms, the initial number of forms and the maximum number of forms in a management form.


1 Answers

This is an adaption of Andrew's Javascript solution, using Django forms the way you'd usually expect.

The form in Django / Python:

class SearchForm(forms.Form):
    type = forms.ChoiceField(choices = ((1, 'One'), (2, 'Two')))

    # Use any form fields you need, CharField for simplicity reasons
    list1 = forms.CharField()
    list2 = forms.CharField()

The template, assuming an instance of SearchForm got passed to the template context with the name 'form':

<script>
function Hide() {
    if(document.getElementById('id_type').options[document.getElementById('id_type').selectedIndex].value == "1") {
         document.getElementById('id_list1').style.display = 'none';
         document.getElementById('id_list2').style.display = '';
    } else {
         document.getElementById('id_list1').style.display = '';
         document.getElementById('id_list2').style.display = 'none';
    }
}

window.onload = function() {
    document.getElementById('id_type').onchange = Hide;
};
</script>

<div>
    {{ form.type.label_tag }}{{ form.type }}
</div>
<div>
    {{ form.list1.label_tag }}{{ form.list1 }}
</div>
<div>
    {{ form.list2.label_tag }}{{ form.list2 }}
</div>
like image 61
Lukas Bünger Avatar answered Sep 23 '22 22:09

Lukas Bünger