1 Template Introduction Sub Html--include
7Template engine-Master Version-include, import the common HTML A. Usage: {% include"pub.html"%}, pub.html can also add {{name}} B. A page can be imported multiple times, and an HTML can have only one master C. Example # Public. html<divclass=" Public"> <divclass="content">{{UserInfo}}</div> </div># app02_test.html {% include"public.html"%} {% include"public.html"%} # Views def test (request):returnRender (Request,'app02_test.html', {'UserInfo':{'K1':'v1','K2':'v2'}})
View Code
2 template use (render data + Call function)
-Templates-receive the rendered data The views parameter is passed: {'UserInfo': {'K1':'v1','K2':'v2'}} Front-end receive can {% forVinchUserinfo.values%} {% ENDFOR%} or {% forKvinchUserinfo.items%} {% ENDFOR%} or {% forKinchUserinfo.keys%} {% ENDFOR%} -The template executes the function, and the function is not the JS function, but the function inside the PY a.{{name|upper}}//name is the rendered data that is passed to the front end in the background, upper is the convert uppercase function
View Code
3 custom functions for template invocation
-Custom Template Filter A. Create folder in app Templatetags b. Create any py file, xx.py fromDjango Import Template Register=template. Library () @register. Filter def my_upper (value):returnValue.upper () c. Import xx.py at the beginning of the template file import {% load xx%} d. Use the function in the template {{'Liuzhipeng'|My_upper}} , the preceding Liuzhipeng is passed to My_upper E for the parameter. Be sure to register app_02-custom Tag A. Create folder in app Templatetags b. Create any py file, xx.py fromDjango Import Template Register=template. Library () @register. Simple_tag def my_concat (Arg1, arg2):returnArg1 +arg2 C. Import xx.py at the beginning of the template file import {% load xx%} d. Use the function tag in the template {% My_concat"Alex" "is SB"%E. Be sure to register app_02-the filter and Simple_tag differ by a. Filter can have up to two parameters B. Filter template Invocation method: {{parameter|Function}} C. Unlimited number of simple_tag parameters D. Simple_tag usage: {% function parameter parameter%} e. {%ifName|my_bool%You can use filter this way, and tag cannot f. Simple_tag reverse-generate URL {% URL'URL Aliases'%} g. can {%ifName|my_bool%}, but simple_tag not.
View Code
[Oldboy-django] [2 in-depth Django]django template usage function