您当前的位置: 首页 >  Python

轻松学Python

暂无认证

  • 2浏览

    0关注

    317博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Python教程:十分钟入门Django模板

轻松学Python 发布时间:2019-08-31 16:39:41 ,浏览量:2

模板

模板注释

单行注释:

{# 注释内容 #}

多行注释:

{% comment %}
注释内容
{% endcomment %}

注意,模板注释和html注释不同,html注释后浏览器检查还能看到朱时候的代码,但模板注释不能被看到。

 

模板的功能

产生html,控制页面上展示的内容。模板文件不仅仅是一个html文件。 模板文件包含两部分内容: 1)静态内容:css,js,html。 2)动态内容:用于动态去产生一些网页内容。通过模板语言来产生。

 

模板文件的使用

通常是在视图函数中使用模板产生html内容返回给客户端。 a)加载模板文件 loader.get_template 获取模板文件的内容,产生一个模板对象。 b)定义模板上下文 RequeseContext 给模板文件传递数据。 c)模板渲染产生html页面内容 render 用传递的数据替换相应的变量,产生一个替换后的标准的html内容。

不用render自己渲染模板示例/ 给一个函数使用templates模板

def index(request):
    # 1.加载模板文件,获取一个模板对象
    temp = loader.get_template('booktest/index.html')
    # 2.定义模板上下文,给模板文件传数据
    # context = RequestContext(request, {})  # 在django1.11中不能使用这种方法,只能直接定义一个字典
    context = {}
    # 3.模板渲染,产生一个替换后的html内容
    res_html = temp.render(context)
    # 4.返回应答
    return HttpResponse(res_html)

自定义render渲染模板示例/ 抽离出templates渲染函数给多个视图函数使用

def my_render(request, template_path, context={}):
    # 1.加载模板文件,获取一个模板对象
    temp = loader.get_template(template_path)
    # 2.定义模板上下文,给模板文件传数据
    # context = RequestContext(request, context)
    # context = {}
    # 3.模板渲染,产生一个替换后的html内容
    res_html = temp.render(context)
    # 4.返回应答
    return HttpResponse(res_html)


def index(request):
    # return render(request, 'booktest/index.html')
    return my_render(request, 'booktest/index.html')
模板文件加载顺序

1)首先去配置的模板目录下面去找模板文件。 2)去INSTALLED_APPS下面的每个应用的templates去找模板文件,前提是应用中必须有templates文件夹。

# index2页面未创建
def index2(request):
    """模板文件的加载顺序"""
    return my_render(request, 'booktest/index2.html')

报错结果

 

模板语言

模板语言简称为DTL。(Django Template Language)

模板变量

模板变量名是由数字,字母,下划线和点组成的,不能以下划线开头。 使用模板变量:

{{模板变量名}}

模板变量的解析顺序: 例如:{{ book.btitle }}

  • 1)首先把book当成一个字典,把btitle当成键名,进行取值book['btitle']
  • 2)把book当成一个对象,把btitle当成属性,进行取值book.btitle
  • 3)把book当成一个对象,把btitle当成对象的方法,进行取值book.btitle

例如:{{book.0}}

  • 1)首先把book当成一个字典,把0当成键名,进行取值book[0]
  • 2)把book当成一个列表,把0当成下标,进行取值book[0]

如果解析失败,则产生内容时用空字符串填充模板变量。使用模板变量时,.前面的可能是一个字典,可能是一个对象,还可能是一个列表。

使用模板变量示例

模板变量函数

def temp_var(request):
    """模板变量"""
    my_dict = {"title": "字典键值"}
    my_list = [1, 2, 3]
    book = models.BookInfo.objects.get(id=1)
    # 定义模板上下文
    context = {'my_dict': my_dict, 'my_list': my_list, 'book': book}
    return render(request, 'booktest/temp_var.html', context)

模板变量html代码(booktest/temp_var.html)




    
    模板变量
    


使用字典属性:{{ my_dict.title }} 
使用列表元素:{{ my_list.1 }} 
使用对象属性:{{ book.btitle }}

 

模板标签

模板标签使用格式:

{% 代码段 %}

for循环:

{% for x in 列表 %}
# 列表不为空时执行
{% empty %}
# 列表为空时执行
{% endfor %}

可以通过{{ forloop.counter }}得到for循环遍历到了第几次。

if判断:

{% if 条件 %}
{% elif 条件 %}
{% else %}
{% endif %}

 

关系比较操作符:> < >= { forloop.counter }}--{{ book.btitle }}{ forloop.counter }}--{{ book.btitle }}{ forloop.counter }}--{{ book.btitle }}{ book.btitle }}--{{ book.bpub_date|date:'Y-m-d' }}{ book.btitle|length }}--{{ book.bpub_date }}{ content|default:"hello" }} { book.bpub_date|date:'Y-m-d' }}是正确的,而{{ book.bpub_date|date: 'Y-m-d' }}是错误的。{ book.id }}--{{ book.btitle }}--{{ book.bpub_date|date:'Y-m-d' }}{ book.btitle }}--{{ book.bpub_date }}{ book.id }}--{{ book.btitle }}--{{ book.bpub_date|date:'Y-m-d' }}{ book.btitle|length }}--{{ book.bpub_date }}{ content|default:"hello" }} { block.super}} #获取父模板中块的默认内容 重写的内容 {% endblock 块名%}{ block.super }}

关注
打赏
1663832040
查看更多评论
立即登录/注册

微信扫码登录

0.0392s