The Django Book - 第四章 模板2
2013-11-01 00:02
459 查看
模板(相应)使用的几种方式:
1.使用HttpResponse返回字符串HTML
2. 使用普通模板
3.使用文件模板
4.简化文件模板render_to_response
5.简化传入变量;注:会传入所有的局部变量
include模板标签
{% include template_name %} template_name基本上是字符串,也可为变量
模板继承
{% extends template_name %}
1.必须为第一个模板标记
2. 父模板中标签{% block %}尽量多
3. {% block.super %} --还不知道如何使用
4. template_name基本上是字符串,也可为变量
模板加载顺序:
1.'django.contrib.staticfiles.finders.FileSystemFinder', TEMPLATE_DIRS中定义的路径
2.'django.contrib.staticfiles.finders.AppDirectoriesFinder', 各个应用下templates文件夹下
其他:
1.文件中使用__file__为该python脚本的路径+文件名; 在控制台无效
2. locals():局部变量字典集合
1.使用HttpResponse返回字符串HTML
from django.http import HttpResponse def current_datetime(request): now = datetime.datetime.now() html = "<html><body>It is now %s.</body></html>" % now return HttpResponse(html)
2. 使用普通模板
from django.http import HttpResponse from django.template import Template, Context from django.template.loader import get_template def current_datetime(request): now = datetime.datetime.now() t = Template('<html><body>It is now {{ current_time }}.</body></html>') c = Context({'current_time': now}) html = t.render(c) return HttpResponse return HttpResponse(htm;)
3.使用文件模板
from django.http import HttpResponse from django.template import Template, Context def current_datetime(request): now = datetime.datetime.now() t = get_template('current_datetime.html') c = Context({'current_time': now}) html = t.render(c) return HttpResponse(html)
4.简化文件模板render_to_response
form django.shortcuts import render_to_response def current_datetime(request): now = datetime.datetime.now() return render_to_response('current_datetime.html', {'current_time': now})
5.简化传入变量;注:会传入所有的局部变量
from django.shortcuts import render_to_response def current_datetime(request): current_time = datetime.datetime.now() return render_to_response('current_datatime.html', locals())
include模板标签
{% include template_name %} template_name基本上是字符串,也可为变量
模板继承
{% extends template_name %}
1.必须为第一个模板标记
2. 父模板中标签{% block %}尽量多
3. {% block.super %} --还不知道如何使用
4. template_name基本上是字符串,也可为变量
模板加载顺序:
1.'django.contrib.staticfiles.finders.FileSystemFinder', TEMPLATE_DIRS中定义的路径
2.'django.contrib.staticfiles.finders.AppDirectoriesFinder', 各个应用下templates文件夹下
其他:
1.文件中使用__file__为该python脚本的路径+文件名; 在控制台无效
2. locals():局部变量字典集合
相关文章推荐
- The Django Book - 第四章 模板
- The Definitive Guide To Django 2 学习笔记(八) 第四章 模板 (四)基本的模板标签和过滤器
- The Django Book的一个view函数改用django模板实现
- The Django Book:第4章 Django模板系统
- The Definitive Guide To Django 2 学习笔记(七) 第四章 模板 (三)使用模板系统
- The Django Book学习笔记 04 模板
- The Django Book【第4章 Django模板系统】
- The Definitive Guide To Django 2 学习笔记(五) 第四章 模板 (一)基本模板系统
- 20121024 The django book 笔记 模板
- 翻译www.djangobook.com之第四章:Django模板系统
- The Definitive Guide To Django 2 学习笔记(六) 第四章 模板 (二)使用模板系统
- The Django Book中文翻译版【第二版】
- 20121030 The django book 笔记 数据模型高级进阶
- 【updating】python读书笔记-The Django Book2.0(for django1.4)
- 很值得看看的中文翻译The Django Book
- The Django Book 第一章【Django介绍】
- The Django Book
- python读书笔记-django架站过程总结(from the django book)
- 《the django book》记录 :admin声明在管理网站里的体现
- 翻译www.djangobook.com之第十章:深入模板引擎