django custom filters and labels

Keywords: Python Android iOS Java

Custom filters and labels are written in the python file (.py), so first create a new folder name, and then create a new folder under this folder, the name is generally written template tags, write this name, in order to facilitate the connection with the system, and finally create a new Python file under template tags, take a name This is common_extra.py, and then register the new outermost folder name into the APPS in settings.py
Filters and labels are usually written in templates, so prepare a template and some data first.

Create a new template and name it test2.html

views.py in

context(context)The data in the test2.html Stencil

def func(request):
return render(request,'test/test2.html', context={


    'name':'liping',
    'age':20,
    'list':[1,3,11,20,30,33,0,0.9],
    'time_format':'%Y %m %d %H:%M:%S',
    'showList':['android', 'ios', 'java', 'ai', 'python'],


})

test2.html in

Import custom file names on top
{% load common_extra %}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    < title > custom tags and filters </title >
</head>
<body>

Custom Sorting Filter
{{ list|my_sort }} <br>
Customized Uppercase Filter
{{ name|myUpper }} <br>
Custom Replacement String Character Filter
{{ name|myReplace:'i' }} <br>

<br>

Custom access to current time label
{% current_time %}

<br>

Customize Getting Current Time Label Passing Parameters
{% current_time2 '%Y %m %d %H:%M:%S' %}

<br>

A custom simple addition filter
{% add 10 100 %}

<br>

A custom filter to get the current time, using context data
{% current_time3 %}

<br>

Customizing Inclusion Labels Using showTest.html Template
{% showHtml %}

<br>

Customize inclusion tags using showTest.html template transfer parameters
{% showHtml2 'android' %}

<br>

Customize inclusion tags using showTest.html template using context data
{% showHtml3 %}


</body>
</html>

common_extra.py file

Import template package
from django import template
//Getting Library Objects
register = template.Library()


# Custom filter

//Custom Sorting
@register.filter
def my_sort(value):
    a = sorted(value)
    return a

//Custom capitalization
@register.filter('myUpper')
def myUp(value):
    return value.upper() + '  custom'


//Custom substitution characters
@register.filter
def myReplace(value, arg):
    return value.replace(arg, 'k')




# Customize Simple Labels

//Import time packet
import datetime

//Customize Simple Tags without Parameters
@register.simple_tag
def current_time():

    format_string = "%Y year %m month %d day %H:%M:%S"
    return datetime.datetime.now().strftime(format_string)


//With parameters
@register.simple_tag
def current_time2(timeFormat):

    return datetime.datetime.now().strftime(timeFormat)



@register.simple_tag
def add(a, b):
    c = a + b
    return c


//Using contextual data    
@register.simple_tag(takes_context=True)#Allow use of context
def current_time3(context):#This parameter name must be context
    format = context.get('time_format')
    return datetime.datetime.now().strftime(format)



# Custom Inclusion Label
//Including tags is one thing to be displayed in one module and rendered in another module.

# No parameters
@register.inclusion_tag('test/showTest.html')
def showHtml():
    arr = ['java', 'ios', 'python', 'web', 'php', 'c']
    
    return {'array':arr}


#Including parameters
@register.inclusion_tag('test/showTest.html')
def showHtml2(arr):

    return {'array':arr}


# Use contextual data
@register.inclusion_tag('test/showTest.html', takes_context=True)
def showHtml3(context):
    list = context.get('showList')
    return {'array':list}

showTest.html in

{% for i in array %}

    <button>{{ i }}</button><br>

{% endfor %}

Posted by Moon-Man.net on Fri, 09 Aug 2019 01:30:57 -0700