【问题标题】:How can I add javascript file to my ModelForm in django?如何在 django 中将 javascript 文件添加到我的 ModelForm?
【发布时间】:2016-06-29 13:58:51
【问题描述】:

我想根据下拉列表的选择显示一个单独的 div 标签,我正在使用 ModelForm 创建我的模板。我不确定如何在我的 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”。

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>

我不知道如何在我的 ModelForm 中使用 Javascript,请帮忙。

【问题讨论】:

  • 这个问题太宽泛了。您可以像创建静态 HTML 表单一样使用 JavaScript。按 ID 或name 定位元素,并根据需要显示/隐藏它们。

标签: javascript jquery html django django-bootstrap3


【解决方案1】:

Django 方式会是

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

然后像{{&lt;yourformname&gt;.media}}一样将它包含在你的模板中,然后在customer_detail.js中写下你的javascript代码

【讨论】:

  • 这是新事物。我不知道。谢谢。
  • @itsecurity 什么不起作用..!您需要在该 JS 文件中编写所有 javascript,就像答案之一所建议的那样。
  • 它的工作,我链接了位于我的应用程序目录中的 js 文件,但我的静态文件位于我的应用程序目录之外的 staic 目录中。感谢您的帮助。
  • 我有另一个 html 模板,它没有模型表单,只是一个视图,我包含了静态文件,但它不起作用。
  • 我的所有表单都有一个模态窗口,这真是太棒了,我不知道
【解决方案2】:

我没有对此进行测试,但它应该是这样的(我在这里使用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>

【讨论】:

  • jquery 函数工作正常。感谢您的帮助。
  • 有了这个,它只有在页面上使用表单的单个实例时才有效,因为我们通过它们的 id 获取 html 元素。有没有办法修改解决方案,以便它可以在同一页面中使用相同类型的多个表单?也许通过包含表单前缀?
猜你喜欢
  • 2011-09-12
  • 1970-01-01
  • 1970-01-01
  • 2018-10-13
  • 2011-06-07
  • 2015-06-25
  • 2018-03-14
  • 2011-12-28
  • 2012-08-27
相关资源
最近更新 更多