如何在 django 中添加 javascript 文件到我的 ModelForm 中

How can I add javascript file to my ModelForm in django?

本文关键字:文件 我的 ModelForm javascript 添加 django      更新时间:2023-09-26

我想根据下拉列表的选择显示单独的div标签,我正在使用ModelForm来创建我的模板。我不确定如何在我的模型表单中添加javascript。

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']

当从"支持"选项中选择amc时,我想显示"amc_date"和"amc_product",当从"支持"选项中选择"保修"时,则显示"warranty_date"和"warranty_product_list"。

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>

我不确定如何在我的模型表单中使用Javascript,请帮忙。

Django方式将是

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

然后像{{<yourformname>.media}}一样将其包含在模板中,并在该customer_detail.js中编写JavaScript代码

我还没有测试过这个,但这就是它应该的样子(我在这里使用jQuery)。

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>