美文网首页
2018-11-20 Django 自定义模板过滤器

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

作者: 多吃水果少吃肉 | 来源:发表于2018-11-20 15:10 被阅读0次

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

    步骤:

    • 在 app 下面新建 templatetags

    • 创建想要的过滤器文件 split.py

    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)
    
    
    • 然后在模板中 load 即可 {% load split %}, 然后就可以使用啦
    {% for name in sample.clinical_data.all %}
        {{ name|split:'/'|last }}
    {% endfor %}
    

    炒鸡简单!

    相关文章

      网友评论

          本文标题:2018-11-20 Django 自定义模板过滤器

          本文链接:https://www.haomeiwen.com/subject/clhxqqtx.html