Python Django框架模板渲染功能示例
作者:houyanhua1 时间:2023-03-12 17:39:03
本文实例讲述了Python Django框架模板渲染功能。分享给大家供大家参考,具体如下:
项目名/settings.py(项目配置,配置模板文件的路径):
import os
# 项目目录的绝对路径
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')], # 设置模板文件目录(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',
],
},
},
]
应用名/views.py(视图,使用模板的详细步骤):
from django.http import HttpResponse
from django.template import loader,RequestContext
# 定义视图函数 (必须传递HttpRequest参数) (需要在urls.py中配置路由)
def index(request):
# 1.获取模板
template = loader.get_template('应用名/index.html') # 需要在settings.py中配置模板目录
# 2.定义上下文 (分配的模板变量)
context = RequestContext(request,{'title':'图书列表','list':range(10)})
# 3.渲染模板并返回 (生成html内容)
return HttpResponse(template.render(context))
应用名/views.py(视图,使用模板的简单写法,render):
from django.shortcuts import render # 导入render
# 视图函数
def index(request):
context = {'title':'图书列表','list':list(range(1,10))} # 字典,分配给模板的变量
return render(request,'应用名/index.html',context) # render对模板的使用步骤进行了封装。 第三个参数可以省略不写
templates/应用名/index.html(模板文件,需要手动创建,settings.py中配置模板路径):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>模板文件</title>
</head>
<body>
<h1>这是一个模板文件</h1>
使用模板变量:<br/>
{{ title }}<br/>
使用列表:<br/>
{{ list }}<br/>
for循环:<br/>
<ul>
{% for i in list %}
<li>{{ i }}</li>
{% endfor %}
</ul>
</body>
</html>
模板变量使用:{{ 模板变量名 }}
模板代码段:{% 代码段 %}
for循环:
{% for i in list %}
{% empty %}
如果遍历的list是空列表,就会显示该内容。
{% endfor %}
模板文件的加载(查找)顺序:
希望本文所述对大家基于Django框架的Python程序设计有所帮助。
来源:https://blog.csdn.net/houyanhua1/article/details/84936822
标签:Python,Django,模板渲染
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
pycharm 2019 最新激活方式(pycharm破解、激活)
2021-09-15 13:07:01
![](https://img.aspxhome.com/file/2023/9/89219_0s.jpg)
Git的简单理解及基础操作命令详解
2023-01-17 11:03:35
k8s在go语言中的使用及client 初始化简介
2024-02-03 18:10:51
异步加载Google Adsense 更新到Wordpress 2.62
2008-09-11 13:09:00
用python打印菱形的实操方法和代码
2023-03-18 19:17:18
sqlalchemy实现时间列自动更新教程
2021-08-18 20:12:58
django列表筛选功能的实现代码
2021-06-23 08:48:57
Django+Ajax+jQuery实现网页动态更新的实例
2023-02-11 03:23:19
利用python绘制中国地图(含省界、河流等)
2021-12-19 03:19:59
![](https://img.aspxhome.com/file/2023/9/128209_0s.jpg)
Python+appium框架原生代码实现App自动化测试详解
2023-02-26 11:59:32
![](https://img.aspxhome.com/file/2023/9/127929_0s.jpg)
python邮件中附加文字、html、图片、附件实现方法
2022-10-28 01:17:13
python求众数问题实例
2022-02-06 22:25:40
Python实现孤立随机森林算法的示例代码
2021-11-25 14:38:24
![](https://img.aspxhome.com/file/2023/6/131346_0s.jpg)
Go语言设计模式之实现观察者模式解决代码臃肿
2024-05-25 15:11:17
Windows10下mysql 8.0.19 winx64安装教程及修改初始密码
2024-01-26 11:03:37
![](https://img.aspxhome.com/file/2023/3/70463_0s.jpg)
Python 实操显示数据图表并固定时间长度
2021-06-08 02:58:57
![](https://img.aspxhome.com/file/2023/7/107557_0s.gif)
python解决汉字编码问题:Unicode Decode Error
2022-12-18 21:22:53
用python写一个windows下的定时关机脚本(推荐)
2022-03-01 10:26:37
用python自动生成日历
2022-08-28 14:28:58
![](https://img.aspxhome.com/file/2023/4/72174_0s.png)
Python词法结构
2022-01-10 00:33:21