加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

python的Web框架,Django自定义过滤器及标签

发布时间:2020-12-15 17:16:16 所属栏目:大数据 来源:网络整理
导读:h3 id="代码布局" data-source-line="1"span style="font-size: 18pt;"代码布局 p data-source-line="2"有的时候框架给的过滤器不够用,需要自定义一些过滤器,所以就需要我们自己来定义一些过滤器等 h5 id="自定义代码放置的路径" data-source-line="3"span

<h3 id="代码布局" data-source-line="1"><span style="font-size: 18pt;">代码布局
<p data-source-line="2">有的时候框架给的过滤器不够用,需要自定义一些过滤器,所以就需要我们自己来定义一些过滤器等


<h5 id="自定义代码放置的路径" data-source-line="3"><span style="font-size: 18px;">自定义代码放置的路径
<p data-source-line="4">某个app特用(独有)的 - app 目录下的 templatetags文件夹内(文件夹内需要有init.py文件)


<p data-source-line="7">公用的 - 再创建一个新的app,然后在新的app中创建templatetags(固定的单词写法)文件夹,但是需要在setting文件中的INSTALL_APPS中注册,然后就可以应用了


<p data-source-line="7">?



func(value,arg= map = :(, :(, 实现需要返回的值

<h5 id="注册" data-source-line="35">注册


<p data-source-line="36">通过<span class="Apple-converted-space">?django.template.Library<span class="Apple-converted-space">?它的实例的filter<span class="Apple-converted-space">?方法


<div class="cnblogs_code">

          -         - 
     
      django.template      
     
     register = Library()

<p data-source-line="49">第一种方法:


<div class="cnblogs_code">

               
     
     
     register.filter(     

第二种方法:通过装饰器方法来注册

@register.filter(name=

<h5 id="模板中使用" data-source-line="68">模板中使用
<ul data-source-line="69">

  • 第一步,需要先load一下,通过python模板名,在网页中load在templatetags文件夹下的这个过滤器函数的py文件 需要查看下settings.py文件中的INSTALLED_APPS 是否有注册,如没有就需要注册。
  • {% load customer_filters %}

      django.template.Library.simple_tag()

    datetime django.template register = datetime.now().strftime(format)

    <p data-source-line="98">注册的第一种方式:


    <div class="cnblogs_code">

         
         register.simple_tag(current_time,name=)

    <p data-source-line="103">注册的第二种方式:


    <div class="cnblogs_code">

         
         @register.simple_tag(name=)

    <h5 id="在html中使用" data-source-line="110">在html中使用


    <div class="cnblogs_code">

     
     {% load customer_tags%}
    
    
    {% current

    <pre data-source-line="111"><code class="hljs"><span class="zh-hans"><span class="zh-hans"><span class="zh-hans">?
    <p data-source-line="119">引用上下文变量,此处简单标签的参数,也可以使用成views中render传递到模板中的context里面的数据。<span class="Apple-converted-space">?需要在simple_tag中,设置参数take_context=True


    <div class="cnblogs_code">

          @register.simple_tag(name=,takes_context=     
           current_time(context,format_str): 

      {% i ls %
    • {{ i }}
    • {% endfor %

    <p data-source-line="147">内容写在简单标签customer_tags的文件中


    <div class="cnblogs_code">

         
          django.template      register = 
         
         @register.inclusion_tag(     
                   {:value}

    <h5 id="在html中使用-2" data-source-line="159">在html中使用
    <div class="cnblogs_code">

     
     {% load customer_tags% 
     
     {% show_list_as_ul stu.course %}

    <p data-source-line="168">例子: 原本的课程展现方式<span class="Apple-converted-space">?


    <p data-source-line="168"><span class="Apple-converted-space">

    <img src="https://www.52php.cn/res/2019/03-02/09/546d7527217fdffdac2dc0687241e88b.png" alt="">

    <p data-source-line="172">通过包含标签渲染后: 根据自定义的方式展现出来。<span class="Apple-converted-space">?


    <p data-source-line="172"><span class="Apple-converted-space">

    <img src="https://www.52php.cn/res/2019/03-02/09/8751b7fa41fe9dd2576c29a305ecb4fa.png" alt="">

    <h5 id="带参数的包含标签-inclusion" data-source-line="177"><span style="font-size: 14pt;">带参数的包含标签 inclusion:
    <p data-source-line="178">标签函数


    <div class="cnblogs_code">

          django.template      register =     
         @register.inclusion_tag(               {:value,:style}

    {% style == %
    => {% i ls %
    {% endif % {% endif %}

    <p data-source-line="203">html中的用法


    <pre data-source-line="204"><code class="hljs"> <span class="zh-hans">标签函数+<span class="zh-hans">空格+<span class="zh-hans">数据+<span class="zh-hans">空格+<span class="zh-hans">标签中的参数
    <span class="hljs-tag"><<span class="hljs-name">td>{% show_list_as_ul stu.course ''%}<span class="hljs-tag"></<span class="hljs-name">td>

    <p data-source-line="208">渲染之后的效果:<span class="Apple-converted-space">?


    <p data-source-line="208">

    <img src="https://www.52php.cn/res/2019/03-02/09/d5c94b70055121633d7405b7854be7ff.png" alt="">

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读