django--模板,模板表情,过滤器

模板:T

可以根据在视图中传递字典数据,传递给模板并动态生成网页

模板变量

语法: {{变量名}}

取出变量名所对应的值,变量名来源于视图函数中传递的字典key

# urls.py
urlpatterns = [
    path('index',index),
]

# views.py
def index(request):
    data = {'name':'花花','age':18}
#     创建templates模板文件夹,创建html文件
    return render(request,'index.html',data) ## 渲染的视图文件名是index.html 传递的数据是定义的data字典
{# templates/index.html #} 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h2>姓名:{{name}}</h2>
	<h2>年龄:{{age}}</h2>
</body>
</html>

配置settings文件

#settings.py
# 模板
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates')], # 告诉django的模板引擎在找模板的时候根据这个路径去找 
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

过滤器

过滤器本质就是一个函数,django提供了一些过滤器可以在模板中使用

语法: {{变量名| 过滤器名字}}

# urls.py
 path('filter_test',filter_test),
 #views.py
# 过滤器,本质是一个函数
def filter_test(request):
    data = {
        'slogan':'mike',
        'lan':'python'
    }
    return render(request,'filter.html',data)

# filter.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>过滤器</title>
</head>
<body>
<!--首字母大写-->
<!--slogan = capfirst(slogan)-->
<h2>{{slogan | capfirst }}</h2>
<!--默认值,当模板变量取不到值的情况下,default过滤器才会生效-->
<h2>{{ slogan | default:'空'  }}</h2>
<!--链式过滤器-->
<h2>{{ slogan  | default:'life is short'| capfirst  }}</h2>
</body>
</html>

模板标签

语法: {%标签名%}
在模板中进行逻辑处理

if for

语法:{%for 变量名 in 要循环的变量%}{%endfor%}

# urls.py
path('index2',index2),
# views.py
def index2(request):
   data = {
        'hobby1':['看电影','学python','锻炼'], # 列表
        'hobby2':('看电影','学python','锻炼'), # 元组
        'hobby3':{'看电影','学python','锻炼'}, # 集合
        'score':{'小1':90,'小2':75,'小4':91},
        'test' : [],
        'status':True,
        'num':10
    }
    return render(request,'index2.html',data)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <!--{{hobby}}-->
    <ul>
        {% for i in hobby  %}
        <!--forloop.counter 对外层for循环进行计数,从1开始,每循环一次,就+1-->
            <!--<li>{{forloop.counter}}  {{i}}</li>-->
            
         <!--forloop.counter0 对外层for循环进行计数,从0开始,每循环一次,就+1-->
            <!--<li>{{forloop.counter0}}  {{i}}</li>-->
            
        <!--forloop.revcounter 反向计数,第一次获取的是最后一个元素对应的序号-->
        <!--<li>{{forloop.revcounter}}  {{i}}</li>-->
        
        <!--forloop.revcounter0 反向计数,第一次获取的是最后一个元素对应的序号-1-->
            <!--<li>{{forloop.revcounter0}}  {{i}}</li>-->
            
        <!--forloop.first 判断是不是第一次循环,如果是则返回True,反之False-->
            <!--<li>{{forloop.first}}  {{i}}</li>-->
            
        <!--forloop.last 判断是不是最后一次循环,如果是则返回True,反之False-->
           <li>{{forloop.last}}  {{i}}</li>
           
        {%endfor%}
    </ul>

   

    <ul>
        {%for i in score%}
        <!--循环字典的时候,默认获取到的是key值-->
            <li>{{i}}</li>
        {%endfor%}

          {%for i in score.values%}
        <!--通过字典.values获取所有value值-->
            <li>{{i}}</li>
        {%endfor%}

        <!--获取key和value  score.items获取keyvalue的对应关系-->
        {%for k,v in score.items%}
            <li>{{k}} {{v}}</li>
        {%endfor%}
    </ul>

   
 <!--如果视图传递的是一个空,可以for结合empty提供用户体验感-->
    <ul>
        {%for t in test%}
            <li>{{t}}</li>
            {%empty%}
            <li>抱歉,没有查询到数据</li>
        {%endfor%}
    </ul>

    <!--if else标签-->
    {%if status%}
        <a href="www.baidu.com">百度一下</a>
    {%else%}
        <a href="www.shiguangkey.com">潭州课堂</a>
    {%endif%}

    {%if age > 20%}
            <a href="www.baidu.com">百度一下</a>
        {%else%}
            <a href="www.shiguangkey.com">潭州课堂</a>
    {%endif%}


 <!--if elif标签-->
    {%if age > 20%}
        <a href="www.baidu.com">百度一下</a>
    {%elif age == 15%}
        <a href="www.shiguangkey.com">潭州课堂</a>
    {%else%}
        <a href="www.sina.com">新浪</a>
    {%endif%}


</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值