zoukankan      html  css  js  c++  java
  • django10 使用自定义标签配置说明

    1)、在app目录下建目录templatetags【不可改名】目录,然后在该目录下建一个空的__init__.py

    2)、mytags.py

           在templatetags下建一个mytags.py,添加:

    from django import template
    register = template.Library()
    

          添加一个函数,用于html页面调用。例如:

    import re , time , datetime,os,sys
    from lykops.settings import BASE_DIR
    
    @register.filter
    def is_image(value):
    
        value = str(value)
        #请使用str,否则页面报错
    
      full_filename = BASE_DIR + '/' + value if re.search('.jpg$' , value) : if os.path.exists(full_filename) and os.path.isfile(full_filename) : return '<img alt="image" src="/' + value + '" />' else : return value else : return value
    
    

    3)、html页面应用

    在html页面上添加{% load mytags %}

    调用方式

    				{% autoescape off %}
    				    <td>{{ file |is_image  }}</td>
    				{% endautoescape %}

    注意:{% autoescape off %}是为了html对这段代码<td>{{ file |is_image }}</td>不转义

    
    
    
    
    
       
    
  • 相关阅读:
    又回来咯
    4 20210412-1 原型设计作业
    3 20210405-1 案例分析作业
    202103226-1 编程作业
    阅读任务
    准备工作
    原型设计作业
    案例分析
    编程作业
    构建之法读后感
  • 原文地址:https://www.cnblogs.com/lykops/p/7348044.html
Copyright © 2011-2022 走看看