【发布时间】:2015-02-05 14:04:57
【问题描述】:
我正在寻找类似于python的startswith的方法/方式。 我想做的是链接表中以“i-”开头的一些字段。
我的步骤:
-
我创建了过滤器,它返回真/假:
@app.template_filter('startswith') def starts_with(field): if field.startswith("i-"): return True return False
然后将其链接到模板:
{% for field in row %}
{% if {{ field | startswith }} %}
<td><a href="{{ url_for('munin') }}">{{ field | table_field | safe }}</a></td>
{% else %}
<td>{{ field | table_field | safe}}</td>
{% endif %}
{% endfor %}
不幸的是,它不起作用。
第二步。我在没有过滤器的情况下完成了,但是在模板中
{% for field in row %}
{% if field[:2] == 'i-' %}
<td><a href="{{ url_for('munin') }}">{{ field | table_field | safe }}</a></td>
{% else %}
<td>{{ field | table_field | safe}}</td>
{% endif %}
{% endfor %}
这可行,但该模板正在发送不同的数据,并且仅适用于这种情况。我在想 [:2] 可能有点问题。
所以我尝试编写过滤器,或者可能有一些我在文档中跳过的方法。
【问题讨论】:
-
“它不起作用”是什么意思?
-
内部服务器错误