2018-11-20 Django 自定义模板过滤器

2018-11-20  本文已影响0人  多吃水果少吃肉

https://docs.djangoproject.com/en/2.1/howto/custom-template-tags/

步骤:

from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()


@register.filter
@stringfilter
def split(string, sep):
    """Return the string split by sep.

    Example usage: {{ value|split:"/" }}
    """
    return string.split(sep)

{% for name in sample.clinical_data.all %}
    {{ name|split:'/'|last }}
{% endfor %}

炒鸡简单!

上一篇 下一篇

猜你喜欢

热点阅读