Django jinja2'BoundField'对象没有属性'__call__'

时间:2015-09-16 08:32:14

标签: python django jinja2

为什么我收到此错误:

'BoundField' object has no attribute '__call__'

我在jinja2.py中有功能:

class SimpleFilterForm(forms.Form):
    name = fields.CharField(label=_(u'Reči'), required=False)
    parent_category = models.ModelChoiceField(label=_(u'Kategorija'), queryset=Category.objects.filter(parent__isnull=True), required=False, empty_label='')
    category = MyMultipleChoiceField(label=_(u'Pod kategorije'), queryset=Category.objects.none(), required=False)
    city = models.ModelMultipleChoiceField(label=_(u'Gradovi'), queryset=City.objects.all(), required=False)
    ad_type = fields.IntegerField(widget=widgets.HiddenInput(), required=False)
    price__gte = fields.DecimalField(label=_(u'Cena od'), required=False, widget=widgets.TextInput(), localize=True)
    price__lte = fields.DecimalField(label=_(u'Cena do'), required=False, widget=widgets.TextInput(), localize=True)
    currency = models.ModelChoiceField(label=_(u'Valuta'), queryset=Currency.objects.all(), to_field_name='code', required=False, empty_label='')

def cat_filter_form_tag(request, parent_category):
    filter_form = SimpleCatFilterForm(parent_category, request.GET)
    return render(request, 'web/category_ads/filter_form.html', {"filter_form": filter_form})

def environment(**options):
    env = Environment(**options)
    env.globals.update({
        'cat_filter_form_tag': cat_filter_form_tag,
    })

    return env

修改

模板:

<div class="sidebox filter-form">
    <h5><span class="fa fa-search"></span> {{ trans('Pretraga') }}</h5>
    <form action="">
        <div class="form-group">
            {{ filter_form.name(class="form-control") }}
        </div>
        <div class="form-group">
            <input type="submit" class="btn btn-primary btn-block" value="{{ trans('Traži') }}">
        </div>
    </form>
</div>

此“名称”字段出错。

在django模板中它可以正常工作,但是使用jinja2它没有。

1 个答案:

答案 0 :(得分:1)

您不会调用字段,也不会通过模板向其添加类;你在表单定义中这样做:

+-------------+-------------+-------------+-------------+
|   Column1   |  Column2    |   Column3   |   Column4   |
+-------------+-------------+-------------+-------------+
|   Mary      |      AA     |     AAA     |      YES    |
+-------------+-------------+-------------+-------------+
|   Mary      |      BB     |     BBB     |      YES    |
+-------------+-------------+-------------+-------------+
|   David     |      AA     |     AAA     |      YES    |
+-------------+-------------+-------------+-------------+
|   David     |      BB     |     BBB     |      YES    |
+-------------+-------------+-------------+-------------+
|   Clara     |      AA     |     AAA     |      NO     |
+-------------+-------------+-------------+-------------+
|   Clara     |      BB     |     BBB     |      NO     |
+-------------+-------------+-------------+-------------+

您只需在模板中使用该属性:

name = fields.CharField(label=_(u'Reči'), required=False, widget=forms.TextInput(attrs={'class': 'form-control'}))

请注意,Jinja和Django模板之间存在无差异