Django USES filter and simple_tag to implement front end custom functions

  • 2020-06-03 07:11:08
  • OfStack

preface

Django template engine provides a function of 1 a sexual function, through the front end can be achieved most of the code logic function, here called sex as a 1, because it only supports function function of the most common case, for example if judgment, ifequal contrast return value, etc., but a little more complex 1 some function does not support the function, for example through template to judge whether the return value is 1 legal digital type, if at this time and don't want to through the backstage view code, we can custom 1 some front-end function function.

Django provides us with two methods, filter and simple_tag, respectively. The following compares the two methods and implements the function function of judging the return value respectively.

The preparatory work

1. The application must be registered in the settings configuration file

2. Create templatetags directory under application directory

3. Create module files in templatetags and import Django internal methods

filter

Create the python module in the templatetags directory, here named app01_func.py, as follows:


from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()


@register.filter
def value_verification(value): # value Parameters passed for the front end 
 try:
  int(value)
  return True
 except:
  return False

After customizing the background functions, you can call the function functions in the template file. The first option is to introduce the background python module in the header of the template file.


{% load app01_func %}

For example, when we need to determine whether the background return value load is a significant number, we can make the following call:


{% if load|value_verification %}
 {{ load }} is a valid int number.
{% else %}
 {{ load }} is letter.
{% endif %}

simple_tag

simple_tag is coded in the same way as filter1, except that the method simple_tag needs to be called in the decorator section


from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()


@register.simple #  I'm gonna change this to simple_tag
def value_verification(value): # value Parameters passed for the front end 
 try:
  int(value)
  return True
 except:
  return False

At the same time, the invocation mode of the front end also needs to be changed


{% value_verification load %}

Passing parameters

-ES65en supports up to two parameter passing

-ES69en supports multiple parameter passing

Parameter passing can be achieved in filter in the following way


{{ load | value_verification:"100"}}

Here, two parameters are passed to the back end, one is load, the other is 100, and the back end also needs to specify good parameters for the function:


def value_verification(value, custom): #  Configure the parameters 
 ...

Multiple parameters can be specified in simple_tag, and the foreground is called as follows:


{% value_verification load 100 200 ... %}

Here, filter can accept no more than two parameters, but if there are too many parameters and you don't want to use simple_tag, you can splice multiple parameters into a string with a specific character and pass it to the back end, which can also get multiple parameters through split.

conclusion

simple_tag and filter can accomplish some things that the template engine cannot accomplish. filter turns the function we specify into a method with return value that can be executed, and simple_tag turns the function function into a tag function, such as if, ifequal, etc., also with different calling methods. The comparison is as follows:


{{ load | value_verification }} # filter
{% value_verification load %} # simple_tag

Because of the difference in invocation mode, if the return value needs to be the judgment basis of if or ifequal, the filter method can only be used here. The stringfilter method can realize that all parameters received can be changed to string type, just like the register decorator 1, which can be referred to the function method. Note that it should be placed under register, otherwise it will not take effect.

conclusion


Related articles: