【问题标题】:Django Error when trying to add new HTML attributes in model form尝试在模型表单中添加新的 HTML 属性时出现 Django 错误
【发布时间】:2011-12-02 18:06:57
【问题描述】:

我有这个模型

 40 class Item(models.Model):
 41     event = models.ForeignKey(Event)
 42     name = models.CharField('Item Name', max_length=50 )
 43     description = models.CharField('Description', max_length=150, blank=True, null=True)
 44     quantity = models.IntegerField(blank=True, null=True)
 45     start = models.DateTimeField('Availability Start Date', blank=True, null=True)
 46     end = models.DateTimeField('Expiry Date', blank=True, null=True)
 47     cost_price = models.DecimalField('Cost Price Per Item', decimal_places=2, max_digits=10, blank=True, null=True)
 48     selling_price = models.DecimalField('Selling Price Per Item', decimal_places=2, max_digits=10, blank=True, null=True)
 49 
 50     def __unicode__(self):
 51         return u"%s" % self.name

还有这个模型形式

 39 class ItemForm(forms.ModelForm):
 40     description = forms.CharField(label='Description', max_length=250, widget=forms.Textarea, required=False)
 41     image = forms.ImageField(label='Item Picture', max_length=50, required=False)
 42     start = forms.DateField(widget=SelectDateWidget, required=False)
 43     end = forms.DateField(widget=SelectDateWidget, required=False)
 55  
 56     class Meta:
 57         model = Item
 58         fields = ('image',
 59                   'name',
 60                   'description',
 61                   'quantity',
 62                   'start',
 63                   'end',
 64                   'cost_price',
 65                   'selling_price',
 66                   )
 67         widgets = {'cost_price': forms.TextInput(attrs={'onChange':'updateSellingPrice()'})}

我正在尝试在cost_price 字段中添加一个新属性,该属性将依次调用下面的 js 函数:

  8 <script type="text/javascript">
  9     function updateSellingPrice() {
 10         $('#id_cost_price').change(function () {
 11             // get cost price
 12             var costPrice = $('id_cost_price').text();
 13             // set selling price
 14             $('$id_selling_price').val(costPrice);
 16         })
 17     }
 18 </script>

在我的模型表单中包含第 67 行 widgets = {'cost_price': forms.TextInput(attrs={'onChange':'updateSellingPrice()'})} 时出现此错误:

Exception Value: Caught ViewDoesNotExist while rendering: Could not import registration.views.item_details. View does not exist in module registration.views.

当我从模型表单中删除第 67 行时,一切正常/加载正常。我认为 Django 错误消息与我的实际错误无关。题外话:我发现 forms.py 文件中的错误往往会引发错误消息,有时会让您误入歧途。

【问题讨论】:

    标签: django django-forms


    【解决方案1】:

    也许您需要从模型创建表单并对其进行自定义,here 是官方文档。

    使用widgets 自定义 HTML 属性

    【讨论】:

      【解决方案2】:

      我的笨蛋。我在做from django.forms import forms 而不是from django import forms

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-24
        • 2013-10-18
        • 2021-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多