在渲染时捕获AttributeError:'DecimalField'对象没有属性'attrs' [英] Caught AttributeError while rendering: 'DecimalField' object has no attribute 'attrs'

查看:715
本文介绍了在渲染时捕获AttributeError:'DecimalField'对象没有属性'attrs'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题之后。我有以下内容:

following on from this question. I have the following:

models.py

models.py

VARIABLE_CHOICES = (
    ('bool', 'On/Off'),
    ('date', 'Date'),
    ('float', 'Number'),
    ('text', 'Text'),
)

class Variable(models.Model):
    template = models.ForeignKey(Template)
    name = models.CharField(max_length=20)
    type = models.CharField(max_length=5, choices=VARIABLE_CHOICES, default=2)
    data = models.CharField(max_length=100, blank=True, null=True)

forms.py

class VariableForm(ModelForm):

    def __init__(self, *args, **kwargs):
        super(VariableForm, self).__init__(*args, **kwargs)

        if self.instance:
            if self.instance.type == 'bool':
                self.fields['data'].widget = BooleanField()
            if self.instance.type == 'date':
                self.fields['data'].widget = DateField()
            if self.instance.type == 'float':
                self.fields['data'].widget = DecimalField()
            if self.instance.type == 'text':
                self.fields['data'].widget = TextInput()
            self.fields['data'].label = self.instance.name

views.py

def template_variables_view(request,tID):
    ...
    templateVariables = Variable.objects.filter(template=template)
    lvForm = []
    for ltVars in templateVariables:
        lvForm.append(VariableForm(instance=ltVars))

我正在模板中骑自行车穿过lvForm中的表单:

and I am cycling through the forms in lvForm like so in the template:

{% for lo in ltvForm %}
    {% for field in lo %}
        <td>{{ field.label }}:</td><td>{{ field }}</td>
    {% endfor %}
{% endfor %}

但我保持得到错误在呈现时捕获AttributeError:'DecimalField'对象没有属性'attrs'

任何想法? / p>

Any ideas?

推荐答案

由于某些原因,您正在将数据字段的小部件设置为字段。你应该设置该字段。

You're setting the data field's widget to a field, for some reason. You should just be setting the field.

if self.instance.type == 'bool':
    self.fields['data'] = BooleanField()

等。

这篇关于在渲染时捕获AttributeError:'DecimalField'对象没有属性'attrs'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆