视图.py
@register.filter(name="avl")
def avl_facilities(obj):
return obj.avl_facilities()
@register.filter
def na_facilities(obj):
return obj.na_facilities()
模型.py
class Model(models.Model):
#some code .....
def avl_facilities(self):
item = ['bar','bank','music','wifi','offers','credit']
avl = []
for i in item:
if getattr(self,i) == True:
avl.append(i)
return avl
def na_facilities(self):
item = ['bar','bank','music','wifi','offers','credit']
na = []
for i in item:
if getattr(self,i) == False:
na.append(i)
return na
html
<div class="facility pad10">
{% for item in data.rest|avl %}
/* data.rest is appropriate instance of model defined above*/
<span class="label label-danger mrg2 pad5 pull-left">
{{item|title}}
</span>
{% endfor %}
</div>
错误
Invalid filter: 'avl_facilities'
怀疑
我无法理解,如果模型中的 avl_facilities 返回正确的可迭代列表,但它不能作为模板过滤器工作,在此先感谢