(django)02 django模板

django模板

1.自定义render

定义

from django.template import loader,RequestContext
def my_render(request, template_path, context_dict={}):
    # 1.加载模板文件,获取一个render对象
    temp=loader.get_template(template_path)
    # 2.定义模板上下文,给模板传递数据
    # context=RequestContext(request,context) # 旧版本可用,新版本报错
    context=context_dict
    context.setdefault('default','myrender') # 可以在自定义的render里设置默认传参
    # 3.模板渲染,产生一个替换后的html
    res_html=temp.render(context)
    return HttpResponse(res_html)

调用

def myrender(request):
    return my_render(request,'myrender.html',{'op':'opreation 777'})

效果

请添加图片描述

2.模板语言 DTL

  • 模板变量
  1. 模板变量名是由数字,字母,下划线和点组成

  2. 注意:不能以下划线开头

  • 模板标签

{% 代码段 %}
#for循环:
#遍历列表:
{% for i in 列表 %}
#列表不为空时执行
{% empty %}
#列表为空时执行
{% endfor %}
#若加上关键字reversed则倒序遍历:
{% for x in 列表 reversed %}
{% endfor %}
#在for循环中可以通过{{ forloop.counter }}得到for循环遍历到几次
#判断语句:
{% if %}
{% elif %}
{% else %}
{% endif %}         
  • 关系比较操作符

        > <> = <= ==!=

注意:在使用关系比较操作符的时候,比较符两边必须有空格

3.自定义filter

(1)settings.py注册

需要首先注册libraries,字典的键是html里用到的变量名,字典的值是filter的相对路径

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates')],
        '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',
            ],
            'libraries':{
                'person_filter':'templates.filters.person_filter',
            }
        },
    },
]

(2)创建自定义filter

person_filter.py

from django import template
register=template.Library()

# 写函数装饰器
@register.filter
def add_name_age(pname,page):
    return '{}-{}'.format(pname,page)
# 设置别名
@register.filter(name='person')
def add_name_age(pname,page):
    return '{}-{}'.format(pname, page)

(3)HTML调用

<h1>{{ default }}</h1>
<p>{{ op }}</p>

<p># 过滤器的示例</p>
{% load person_filter %}
{{ 'alex'|add_name_age:'25' }}
{{ 'bob'|person:'18' }}

(4)效果

请添加图片描述

4.模板继承

(1)父模板

basic.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>这里是父类模板</title>
    <style>
        h1{
            color:blue;
        }
    </style>
</head>
<body>
{% block demo %}
<h1>demo 1!!</h1>
{% endblock %}

{% block demo2 %}
<h1>demo 2!!</h1>
{% endblock %}
</body>
</html>

(2)子模板

extends.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>extention</title>
</head>
<body>
{% extends 'basic.html' %} # 继承basic模板

{% block demo %} # 重写部分block
<h1>重写的demo 1!</h1>
{% endblock %}

</body>
</html>


(3)效果

请添加图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值