[Oldboy-django] [2 in-depth Django]django template usage function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.