我创建了一个简单的自定义过滤器来检查是否选择了“< select >”中的“< option >”。它工作正常,只是它在 pycharm 编辑器上出现错误“名称空间未绑定”并且过滤器以红色突出显示。显然有问题。
这是我的自定义过滤器
cus_filter.py:
from django import template
register = template.Library()
@register.filter
def is_option_selected(value, to_compare):
return "selected" if value == to_compare else ""
我的应用程序:
{% load cus_filters %}
<label>LONG/SHORT</label>
<select class="form-control">
// {{ data.type|is_option_selected:"Short/Long"}} is highlighted red with error: "tooltip Namespace 'data.type|is_option_selected' is not bound. "
<option {{ data.type|is_option_selected:"Short" }}>Short</option>
<option {{ data.type|is_option_selected:"Long" }}>Long</option>
</select>