django-5- custom template filters and label

Original link: http://www.cnblogs.com/wmkpy/p/10419587.html
<<< code layout (where custom code release) >>>
 (1) a specific app
   under 1. General put fixed python app directory file folder named templatetags of duck, if it is the other name, django is Can not find Oh!

   2. re-written in the folder inside the module
  (2) to create a new app, in which add modules (** Note ** To register app duck !!!)
<<< >>> custom filter template
 first create a module, the module name is fixed at customer_filters.py, if it is another name, will run error oh! !

  (1) template filters (in fact, it is the function!)
   1. There are one or two parameters,
    the first parameter is a template variable
    second parameter is the ordinary parameters, you can also do
 (2) registration
    1. django.template.Library filter methods of its instances of
     filter has two parameters
      name: name of the filter, is a string (if you do not write the name, the function name is the default filter name oh!)
      filter_func: function name
      register = Library()
      register.filter(<function>)
   2. decorator
from django.template import Library

register = Library()

@register.filter()
def myfilter(value):
...

  

    Use the template

     
<<< >>> custom template tags
 (1) simple label (nature also function):
   django.template.Library.simple_tag ()
 (2) Create:
   1. Create a python module, the module name is fixed at customer_tags.py, if not the name, but also will complain, really jer picky !!

   2.注册
     @filter.simple_tag(name=<function>)
from django.template import Library

register = Library()

@register.simple_tag()
def tag():
...

  


  ** If you want to get the inside view of the function of context, since the first parameter defines the label function is context
   Secondly let simple_tag = True function in take_context
  **
 (3) Use: also have to load
 (4) inclusion tags:
   to display the data by rendering another template
   used here inclusion_tag ( 'template path') to decorate
   ** This tag can also get context, the same method and simple_tag **
 
 

Reproduced in: https: //www.cnblogs.com/wmkpy/p/10419587.html

Guess you like

Origin blog.csdn.net/weixin_30500289/article/details/94807680