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>不转义

    
    
    
    
    
       
    
  • 相关阅读:
    hdoj_1556Color the ball
    wchar_t与char转换(总结)
    算法艺术——网络最大流
    poj_3268Silver Cow Party
    poj_2352Stars
    BellmanFord模板
    saas模式
    什么是管道
    什么是CMMI
    saas模式
  • 原文地址:https://www.cnblogs.com/lykops/p/7348044.html
Copyright © 2011-2022 走看看