Django 自学笔记兼学习教程第3章第2节——模板语法介绍

点击查看教程总目录

参考:https://docs.djangoproject.com/en/2.2/topics/templates/#the-django-template-language

0 总介绍

官方文档介绍:

A Django template is simply a text document or a Python string

marked-up using the Django template language. Some constructs are

recognized and interpreted by the template engine. The main ones are

variables and tags.

A template is rendered with a context. Rendering replaces variables

with their values, which are looked up in the context, and executes

tags. Everything else is output as is.

The syntax of the Django template language involves four constructs: Variables,Tags,Filters,Comments

我的菜鸡翻译:

Django模板,只是使用Django模板语言标记的,文本文档或Python字符串。

模板引擎可以识别和解释某些结构(主要是变量和标记)。

模板使用context变量渲染:渲染时,替换掉模板的变量(使用context里面对应的值),并执行标记的模板代码(tags里的代码);其他则按原文输出。

Django模板语言的语法包含四种结构: Variables,Tags,Filters,Comments

补充:context变量:实际就是个字典对象

1 Variables(变量)

直接输出context里的变量

写法为:

{{ variable}}

2 Tags(标签)

在模板里实现随心所欲的逻辑。

这个解释很笼统,因为Tags的功能本身很笼统,

Tags能够实现任意的逻辑代码,既能输出变量,也能用作控制结构(if语句或for循环),还能从数据库中获取内容,甚至启用对其他模板标记的访问。

写法为:

{% tags %}

3 Filters(过滤器)

转换变量(Variables)和标签(Tags)参数的值(或者说呈现格式)。

举例如

{{ str_variable|title }}

其中|title就是过滤器,title 过滤器的作用是让str_variable里的单词首字母大写。

4 Comments(注释)

单行注释

{# this won't be rendered #}

多行注释

{% comment "Optional note" %}
<p>This won't be rendered</p>
{% endcomment %}

最新文章

  1. PHP 数组浅析
  2. jquery的css详解(二)
  3. JAVA源码走读(一) HashMap与ArrayList
  4. oracle 数据库学习3 --oracle 数据库中常使用的函数
  5. KnockoutJS 3.X API 第四章(14) 绑定语法细节
  6. 六、通过插件如何创建自己的MEL command
  7. 如何使用github上传自己的开源项目教程
  8. Oracle -&gt;&gt; TRUNC, ROUND, CEIL, FLOOR
  9. Windows 窗体启动和关闭的事件顺序
  10. 织梦DeDeCms列表分页和内容页分页错位解决办法
  11. JQuery执行函数与window.onload函数
  12. AWR报告生成
  13. 用git上传代码到新浪云
  14. centos 环境变量配置
  15. 我的linux学习之路--(二)linux常用命令
  16. MongoDB 分片键的选择与案例
  17. 安装python--环境配置
  18. Redis的搭建和Redis的集群搭建
  19. Spring学习14-源码下载地址
  20. 创建首个 Android 项目

热门文章

  1. 用Flask 实现文件服务器(包含docker版本)
  2. Dart 中的final 和 const
  3. null与undefined到底有啥区别?
  4. 学校选址(ArcPy实现)
  5. js 面向对象 动态添加标签
  6. 常用SQL函数大全
  7. MIPS指令 MIPS架构
  8. 初次认识指针:C语言*p、p以及&amp;p的区别,*p和**p的区别?
  9. EMC测试国家标准GB/T 17626
  10. WPF 排版基础