Django- template engine

Rendering basic data types

# views.py文件
def test(request):

    name = 'nick'
    age = 18
    hobby = ['read','music','sing']
    info = {"name":'nick',"age":18,"hobby":['read','music','sing']}
    users = [{"name":'nick',"age":18},{'name':'egon',"age":20},{"name":"sean","age":19}]

    return render(request,'test.html',{'name':name,"age":age,"hobby":hobby,"info":info,"users":users})

# test.html文件

name:{{ name }}<br>  # 字符串类型渲染 
age:{{ age }}<br>  # 数字类型渲染    
# 列表类型渲染 
<ul>
    <li>{{ hobby.0 }}</li>
    <li>{{ hobby.1 }}</li>
    <li>{{ hobby.2 }}</li>
</ul>
<ul>
    {% for item in hobby %}
        <li>{{ item }}</li>
    {% endfor %}

</ul>
# 字典类型渲染
<ul>
    <li>{{ info.name }}</li>
    <li>{{ info.age }}</li>
    <li>{{ info.hobby }}</li>
</ul>
<ul>
    {% for item in info.keys %}
        <li>{{ item }}</li>
    {% endfor %}
</ul>
<ul>
    {% for item in info.values %}
        <li>{{ item }}</li>
    {% endfor %}
</ul>
<ul>
    {% for key,val in info.items %}
        <li>{{ key }}:{{ val }}</li>
    {% endfor %}
</ul>
# 列表套字典
{% for user in users %}
    {{ user.name }}
{% endfor %}

Master and sub-version

During development, each page has a master at writing style, the use of inheritance in sub-version

# 母版loyout.html
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    {% block mycss %}

    {% endblock %}
</head>
<body>
{% block mycontent %}

{% endblock %}
</body>
{% block myjs %}

{% endblock %}
</html>


# 子版test.html
{% extends 'loyout.html' %}

{% block mycss %}
子版自定制的样式
{% endblock %}

{% block mycontent %}
子版自定制的内容
{% endblock %}

{% block myjs %}
子版自定制的js
{% endblock %}

Import module

In python functions may be written as a module, Import import file with the plurality py; Similarly, in the django may be written in a good page or component module, include the use of a plurality of files by introducing such.

# test.html模块文件
<form action="" method="post">
    用户名:<input type="text">
    密码:<input type="password">
    <input type="submit" value="提交">
</form>

# 在test1.html中导入test.html中的模块
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>test</title>
</head>
<body>
{% include 'test.html' %}
{% include 'test.html' %}
{% include 'test.html' %}
{% include 'test.html' %}
</body>
</html>

And python import module is different in python can only import once, can be imported more than once in django, the import is displayed several times

Built-in functions

# name是后端返回的字符串
{{ name|upper }}  {# 大写 #}
{{ name|lower }}  {# 大写 #}
{{ name|first|upper }}  {# 取首字母,变大写 #}
{{ name|truncatewords:"2"}}  {# 取name中前两个单词#}

Custom Functions

Step one: Create a directory app in templatetags

Step Two: Create any .py file in templatetags directory, such as user.py, custom function in the file

from django import template

register = template.Library()

@register.filter
def my_filter(val, args):
    return val + args

@register.simple_tag
def my_upper(val, args, args1):
    return val + args + args1

Step Three: Call function in html

{%load user %}
                
{# simple_filter #}
{{name | my_filter:'xxx'}}

{# simple_tag #}
{%my_upper "zekai" "sss" 'dsadsadsa'%}

The filter function can only be two arguments, simple_filter function can pass multiple parameters

Guess you like

Origin www.cnblogs.com/863652104kai/p/11366165.html
Recommended