django中tag的用法

在app里建一个子的python包,包含__init__.py,包名为templatetags,里面新建一个tags.py(这个名字可以随意)

from django import template
register = template.Library()


def short_msg(value):
if len(value) > 50:
return value[:50]+" ......"
else:
return value

register.filter('short_msg', short_msg)

在html页面中

{% extends "base.html" %}
{% load message_tags %}

然后在使用的地方

{{ i.answer|short_msg }}

就完成了

原文地址:https://www.cnblogs.com/tuifeideyouran/p/3805475.html