Python Django模板系统详解
作者:久醉绕心弦, 时间:2021-09-05 23:17:20
设置模板路径
在django项目下创建templats文件来存放html文件
为了减少模板加载调用过程及模板本身的冗余代码,Django 提供了一种使用方便且功能强大的 API ,当使用模板加载API时,需要将模板路径告诉框架,在项目settings.py
中设置模板路径,如图:
settings.py
中的BASE_DIR
为项目路径。
在TEMPLATES
中的BIRS
来设置模板路径
templates
下编写index.html
写入如下代码:
!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>hello world!</h1>
</body>
</html>
视图文件view.py中编写如下代码,通过render渲染html文件:
from django.shortcuts import render
# 获取对应模板通过render渲染
def index(request):
return render(request, 'index.html')
结果如下:
模板变量
Django模板中使用{{ }}来表示变量:
{{ 变量名 }}:变量名由字母数字和下划线组成,其值可以是任何数据类型
举例如下:
当模板引擎遇到变量时,会计算该变量,并将其替换为结果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h3>{{ content }}</h3>
<h3>{{ info }}</h3>
</body>
</html>
view.py
中render
渲染时通过context以字典形式传递值:
from django.shortcuts import render
def index(request):
content = 'hello world'
info = {'name': 'test', 'age': 18}
return render(request, 'index.html', context={'content': content, 'info': info})
模板中支持以下语法:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h3>{{ content }}</h3>
<!-- 获取字典中key的值 -->
<h3>{{ info.name }}</h3>
<!-- 通过索引获取列表的值 -->
<h3>{{li.1}}</h3>
<!-- 调用不带参数的方法 -->
<h3>{{ fun }}</h3>
<!-- 获取对象属性 -->
<h3>{{ obj.name }}</h3>
</body>
</html>
view.py:
from django.shortcuts import render
def index(request):
content = 'hello world'
info = {'name': 'test', 'age': 18}
li = [1, 2, 3]
class Obj:
def __init__(self, name):
self.name = name
M = Obj('对象属性:MING')
def fun():
return '方法:fun'
return render(request, 'index.html', context={'content':content,'info': info,'li': li,'fun': fun,'obj': M})
引用静态文件
首先在项目根目录下创建存放静态文件的目录,并在settings中设置路径,如下:
STATIC_URL = '/static/'
为静态文件引用前缀,当引用文件时代表的是文件根目录,如下:
static
代表的是statics
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<!-- 图片 -->
<img src="/static/img/123.jpg" alt="">
</body>
</html>
view.py:
from django.shortcuts import render
def index(request):
return render(request, 'index.html')
来源:https://blog.csdn.net/weixin_42262081/article/details/121129511
标签:Python,Django,模板系统
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SQL Server 2000日志转移实现高可用性
2009-01-20 15:04:00
javascript在线游戏:找相同的图片
2008-03-12 12:18:00
HTML5本地存储初探(三)
2010-03-07 15:49:00
![](https://img.aspxhome.com/file/UploadPic/20103/7/20100131-mheq73uiue19x7q5g9c52ngtwm-29s.png)
高性能PHP框架Symfony2经典入门教程
2023-11-16 02:39:23
python分割和拼接字符串
2023-05-12 05:57:03
利用Python内置库实现创建命令行应用程序
2022-04-26 03:39:19
详解Python列表赋值复制深拷贝及5种浅拷贝
2022-07-16 16:22:41
Python实现对中文文本分段分句
2022-09-16 18:16:50
CSS技巧及常见问题列表
2008-04-06 14:00:00
ORACLE常用数值函数、转换函数、字符串函数
2023-07-21 02:03:40
Python3简单实现串口通信的方法
2022-03-10 03:39:53
jQuery实现同一点击,两个不同链接,指向两个不同的iframe
2010-06-21 10:52:00
Python实现自动发消息自定义内容的操作代码
2023-10-17 18:01:53
![](https://img.aspxhome.com/file/2023/5/63555_0s.jpg)
MS SQL Server中的CONVERT日期格式化大全
2010-08-07 11:31:00
PHP中round()函数对浮点数进行四舍五入的方法
2023-11-23 21:35:24
解析:轻松掌握在 Mac OS X中安装MySQL
2009-01-14 11:51:00
不要使用@import[译]
2009-05-01 12:01:00
![](https://img.aspxhome.com/file/UploadPic/20095/1/01-39s.gif)
python super()函数的基本使用
2022-01-11 05:24:40
Python使用functools模块中的partial函数生成偏函数
2024-01-01 21:26:47
关于Keras模型可视化教程及关键问题的解决
2021-03-19 10:39:07
![](https://img.aspxhome.com/file/2023/1/78771_0s.jpg)