3

I want to display a separate div tags based on the selection of dropdown, I am using ModelForm to create my template. I am not sure how to add a javascript in my ModelForm.

forms.py

class CustomerForm(forms.ModelForm):
    name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Customer Name '}))
    address = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Customer Address'}))
    phone_number = forms.IntegerField(widget=forms.TextInput(attrs={'placeholder': 'Customer Phone Number '}))
    email = forms.EmailField(widget=forms.TextInput(attrs={'placeholder': 'Customer Email'}))
    contact_person = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Contact person'}))
    # amc = forms.BooleanField(widget=forms.TextInput(attrs={'placeholder': 'type "amc" if the customer is in AMC'}))
    amc_date = forms.DateField(widget=forms.TextInput(attrs={'placeholder': 'ex: Jan 20, 1996','id':'disabledInput'}))
    amc_product = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Products listed in AMC'}))
    # warranty = forms.BooleanField(widget=forms.TextInput(attrs={'placeholder': 'Type "warranty" if the customer is in Warranty'}))
    warranty_date = forms.DateField(widget=forms.TextInput(attrs={'placeholder': 'ex: Jan 20, 1996'}))
    warranty_product_list = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Products listed in warranty'}))
    # on_call = forms.BooleanField(widget=forms.TextInput(attrs={'placeholder': 'Type "oncall/on call" if it is On Call'}))
    # support = forms.CharField(widget=forms.TextInput(attrs={'placeholder':'Enter: amc/warranty/oncall'}))
    support = forms.ChoiceField(choices=support_choice, required=True, widget = forms.Select(attrs={'onchange' : 'customer()'}))

class Meta:
    model = Customer
    fields = ['name','address','phone_number','email','contact_person','support','amc_date','amc_product','warranty_date',
              'warranty_product_list']

I want to display the 'amc_date' & 'amc_product' when amc is selected from the options of 'support' and 'warranty_date' & 'warranty_product_list' when 'warranty' is selected from the options of 'support'.

customer_detail.html

<script type="text/javascript" src="{% static 'js/customer_detail.js' %}"></script>

<form method="post" action="" enctype="multipart/form-data">
   {% csrf_token %}
   {{ form|crispy }}
   <input type="submit" class="btn btn-default " value="Submit">
</form>

I am not sure how to use Javascript in my ModelForm, please help.

1
  • This question is way too broad. You'd use JavaScript in the same way you would if you were creating a static HTML form. Target elements by ID or name, and show/hide them as needed. Commented Mar 14, 2016 at 14:22

2 Answers 2

14

Django way will be

class CustomerForm(forms.ModelForm):
    .
    .
    class Media:
        js = ('js/customer_detail.js',)

and then include it like {{<yourformname>.media}} in your template, And write your javascript code inside that customer_detail.js

Sign up to request clarification or add additional context in comments.

4 Comments

@itsecurity what is not working..! you need to write all that javascript inside that JS file like one of answer is suggesting.
Its working, I linked the js file located in my app directory but my static files are located in staic directory which is outside my app directory. thanks for the help.
I have another html template and it do not have a model form just a view, i included the staticfiles and its not working.
I have a modal window for all my forms and this is realy awesome, I didn't know it
3

I have not tested this but this is how it should be (I am using jQuery here).

forms.py

class CustomerForm(forms.ModelForm):
    name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Customer Name '}))
    address = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Customer Address'}))
    phone_number = forms.IntegerField(widget=forms.TextInput(attrs={'placeholder': 'Customer Phone Number '}))
    email = forms.EmailField(widget=forms.TextInput(attrs={'placeholder': 'Customer Email'}))
    contact_person = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Contact person'}))
    # amc = forms.BooleanField(widget=forms.TextInput(attrs={'placeholder': 'type "amc" if the customer is in AMC'}))
    amc_date = forms.DateField(widget=forms.TextInput(attrs={'placeholder': 'ex: Jan 20, 1996','id':'disabledInput'}))
    amc_product = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Products listed in AMC'}))
    # warranty = forms.BooleanField(widget=forms.TextInput(attrs={'placeholder': 'Type "warranty" if the customer is in Warranty'}))
    warranty_date = forms.DateField(widget=forms.TextInput(attrs={'placeholder': 'ex: Jan 20, 1996'}))
    warranty_product_list = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Products listed in warranty'}))
    # on_call = forms.BooleanField(widget=forms.TextInput(attrs={'placeholder': 'Type "oncall/on call" if it is On Call'}))
    # support = forms.CharField(widget=forms.TextInput(attrs={'placeholder':'Enter: amc/warranty/oncall'}))
    support = forms.ChoiceField(choices=support_choice, required=True)

class Meta:
    model = Customer
    fields = ['name','address','phone_number','email','contact_person','support','amc_date','amc_product','warranty_date',
              'warranty_product_list']

customer_detail.html

<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    </head>

    <form method="post" action="" enctype="multipart/form-data">{% csrf_token %}
        {{ form|crispy }}
       <input type="submit" class="btn btn-default" value="Submit">
    </form>

    <script>
    $(function(){
        function hideInputs() {
            $("#id_amc_date").hide();
            $("#id_amc_product").hide();
            $("#id_warranty_date").hide();
            $("#id_warranty_product_list").hide();
        }

        $("#id_support").on('change', function(){
            var val = $("#id_support").val();
            if (val == 'amc') {
                $("#id_amc_date").show();
                $("#id_amc_product").show();
            } else if (val == 'warranty') {
                $("#id_warranty_date").show();
                $("#id_warranty_product_list").show();
            } else {
                hideInputs();
            }
        });

        hideInputs();
    });
    </script>
</html>

2 Comments

The jquery function is working fine. thanks for the help.
With this, it will only work if using a singe instance of the form on the page because we're fetching html elements by their ids. Is there a way to modify the solution so that it will work with multiple forms of the same type in the same page? Maybe by including the form prefix?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.