最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How can I add javascript file to my ModelForm in django? - Stack Overflow

programmeradmin8浏览0评论

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.

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.

Share Improve this question edited Jun 13, 2017 at 19:25 Vini.g.fer 11.9k19 gold badges65 silver badges98 bronze badges asked Mar 14, 2016 at 13:38 itsecurityitsecurity 491 gold badge2 silver badges8 bronze badges 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. – rnevius Commented Mar 14, 2016 at 14:22
Add a ment  | 

2 Answers 2

Reset to default 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

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./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>
发布评论

评论列表(0)

  1. 暂无评论