常用filter : length filesizeformat {{ time| date :"Y-m-d H:i:s"}} safe (防止xss攻击) truncatechars :20(显示位数) default :"str" -------should be reboot------ def fileter app>>templatetags package>> myfilter.py>> from django import template register=template.Library() @register.filter("name=sa") def add_sb(arg): return "{}sa.".format(arg) -----views.py name="song" return render(request,".html"{"name":name}) -----.html {% load filter%} {{ name | sa}} ------------------------------------------ {#静态文件处理#} {% load static %} <link rel="stylesheet" href="{% static 'bootstrap/css/bootstrap.css'%}" > --------------------------------------------------------------------------- {% load static %} <img src="{% static 'github.png' as song %}" alt=""> <img src="{{ song }}" alt=""> <img src="{{ song }}" alt=""> <img src="{{ song }}" alt=""> <img src="{{ song }}" alt=""> -------------templatetags>>---mysimpletag.py-------- #自定义函数 from django import template register=template.Library() @register.simple_tag(name="yimi") def my_sum(arg1,arg2,arg3): return "{} {} {}".format(arg1,arg2,arg3) #获取页面源码 function>>html>>处理数据 @register.inclusion_tag('result.html') def show_result(n): n=1 if n < 1 else int(n) data=["第{}项".format(i) for i in range(1,n+1)] return {"results":data} ------------.html-------------------- {% load mysimpletag %} {% yimi 'song' 'changjiang' 'chongqi' %} {% show_result 10 %} -------------------result.html-------- <ul> {% for choice in results %} <li>{{ choice }}</li> {% endfor %} </ul> ----------------------------------------
自定义filter及基本应用
最新推荐文章于 2023-02-14 11:56:51 发布