Detailed explanation of using filter and simple_tag to define functions for the front end in Django

零下一度
Release: 2017-06-17 10:58:33
Original
1062 people have browsed it

This article mainly introduces you to the implementation method of using filter and simple_tag as the front-end custom function in Django. The article introduces it in detail through the example code, which has certain reference and learning value for everyone. Friends who need it can come and take a look below.

Preface

Django’s template engine provides general functional functions, and most codes can be implemented through the front end The logical function is called general here because it only supports function functions in most common situations, such as if judgment, ifequal comparison return value, etc., but slightly more complex functions are not supported, such as judgment through templates. Whether a return value is a legal numeric type, if we do not want to implement it through the background view code, we can customize some front-end functions.

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

Preparation

## 1. The application must be registered to settings

Configuration file

2. Create the templatetags directory in the application directory


3. Create the module file in templatetags and import Django internal methods


filter

Create a python module in the templatetags directory, named here app01_func.py, with the following content:


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

register = template.Library()


@register.filter
def value_verification(value): # value为前端传递的参数
 try:
  int(value)
  return True
 except:
  return False
Copy after login

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


{% load app01_func %}
Copy after login

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


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

simple_tag

The coding method of simple_tag is the same as that of filter. The difference is that the simple_tag method needs to be called in the decorator part


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

register = template.Library()


@register.simple # 这里修改为simple_tag
def value_verification(value): # value为前端传递的参数
 try:
  int(value)
  return True
 except:
  return False
Copy after login

At the same time, the front-end calling method also needs to be changed to


{% value_verification load %}
Copy after login

Passing parameters

- filter supports up to two parameter passing


- simple supports multiple parameter passing


Parameter passing in filter can be achieved in the following way


{{ load | value_verification:"100"}}
Copy after login

Here is Two parameters are passed to the backend, one is load and the other is 100. The backend must also specify the formal parameters for the function:


def value_verification(value, custom): # 配置好形参
 ...
Copy after login

simple_tag can specify multiple For each formal parameter, the front-end calling method is as follows:


{% value_verification load 100 200 ... %}
Copy after login

The filter here can only accept up to two parameters, but at this time there are many parameters and you do not want to use simple_tag Below, multiple parameters can be spliced ​​into a

string with specific characters and passed to the backend. The backend can also obtain multiple parameters through split method.

Comparison summary

Some things that the template engine cannot be done can be done through simple_tag and filter. Filter turns the function we specified into A method that returns an executable value. simple_tag changes the function function into a tag function, such as if, ifequal, etc. The calling method is also different. The comparison is as follows:

##

{{ load | value_verification }} # filter
{% value_verification load %} # simple_tag
Copy after login

Because of the calling method The difference is that if the return value needs to be used as the basis for if or ifequal judgment, only the filter method can be used here. The stringfilter method can convert all received parameters into string types and refer to the function method like the register decorator. That’s it. Be careful to put it under register, otherwise it won’t take effect.

The above is the detailed content of Detailed explanation of using filter and simple_tag to define functions for the front end in Django. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template