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 }}

就完成了

posted on 2014-06-24 10:02  颓废的悠然  阅读(590)  评论(0编辑  收藏  举报

导航