The Django Book - 第四章 模板2
模板(相应)使用的几种方式:
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 - 第四章 模板2的更多相关文章
- The Django Book - 第四章 模板
使用模板的最基本方式:1.根据原始模板代码字符串创建一个Template对象2. 使用字典创建一套Context变量3. 调用Template对象的render方法,传入Context变量参数 In ...
- The Definitive Guide To Django 2 学习笔记(八) 第四章 模板 (四)基本的模板标签和过滤器
标签 下面的部分概述了常见的Django标签. if/else {%if%} 标签 对一个变量值进行测试,如果结果为true,系统将会显示在{%if%} 和 {%endif%}之间的一切,看个例子: ...
- The Definitive Guide To Django 2 学习笔记(七) 第四章 模板 (三)使用模板系统
接下来,我们开始学习如何使用模板系统,但我们并不和前面说的View相结合,我们的这里的目的是展示模板系统是如何独立于Django框架运行的.下面是在pyhon代码中使用Django模板系统的基础例子: ...
- The Definitive Guide To Django 2 学习笔记(六) 第四章 模板 (二)使用模板系统
模板系统不是django特有的,它是python的一个库,你可以在任何地方使用它. 使用方法: 1.使用 Template()方法创建Template对象.2.调用Template对象的render( ...
- The Definitive Guide To Django 2 学习笔记(五) 第四章 模板 (一)基本模板系统
引入模板系统的原因,view中引入硬编码并非明智的选择,设计上的任何改变都会需要改动代码.python代码和HTML代码应该分开,这是多数Web站点的共识,分开会提高效率. 基本模板系统 Django ...
- Django 笔记(四)模板标签 ~ 自定义过滤器
模板标签: 标签在渲染的过程中提供任意的逻辑 语法: 由{% ... %} 和 {% end... %} 常用标签: with:类似取别名 模版继承: Django模版引擎中最强大也是最复杂的部分就是 ...
- 潭州课堂25班:Ph201805201 django框架 第四课 模板常用标签,模板继承与引用,自定义过渡器 (课堂笔记)
if 语句 判断传入的 name 值 建好这些文件后,对 url 进行配置 在浏览器中访问 for 循环 页面跳转: 通过 name 跳转时要在 urls 文件中为该 path 设置 name 带参 ...
- 第四章:Django 的模板系统(转)
在之前的章节中,你可能觉得例子中视图返回文本有点不妥.即是, HTML 是直接写在 Python 代码中的. 这种做法会导致这些问题: 要做任何设计上的更改就必须改写 Python 代 ...
- 《Django By Example》第四章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:祝大家新年快乐,这次带来<D ...
随机推荐
- 【Hadoop】HDFS笔记(一):Hadoop的RPC机制
RPC(Remote Procedure Call, 远程过程调用)主要面对两个问题: 1.对象调用方式: 2.序列/反序列化机制. Hadoop实现的RPC组件依赖于Hadoop Writable类 ...
- linux的grep命令参数全拼详解
今天为了查找文件中某段字符,找了好久,最后成功使用指令: find . -name "*.cpp" |xargs grep -in “get_itemInfo” | grep -v ...
- 继承映射关系 joinedsubclass的查询
会出现下面这样的错一般是配置文件中的mapping和映射文件中的package路径或者class中的name路径不一致 org.hibernate.MappingException: Unknown ...
- POJ - 3253 Fence Repair 优先队列+贪心
Fence Repair Farmer John wants to repair a small length of the fence around the pasture. He measures ...
- 纯css单选框
1.没有用bootstrap时: .has_sel,.un_sel{display:block; width:16px; height: 16px; border: 1px solid #B06A50 ...
- 算法学习--Day9
继上一次完成最小生成树后,这次我开始准备最短路径的程序. 最短路分为两种算法,第一个为Floyd算法,第二个为Dijkstra. 简单来说,Floyd是以点为参照对象,它使用三层循环求解当前图中所有点 ...
- css div平移淡入淡出
<!DOCTYPE html> <html> <head> <style> div { width:100px; height:100px; backg ...
- 如何将含有byte数据项的结构存入MongoDb
我们知道MongoDb不支持byte(BsonType中根本没有定义byte), 但是在实际生产环境中数据结构(特别是远古时代的数据结构)往往包含byte数据项. 这时候无法保存原有的数据结构,一般会 ...
- Educational Codeforces Round 20 C(math)
題目鏈接: http://codeforces.com/problemset/problem/803/C 題意: 給出兩個數n, k, 將n拆分成k個數的和,要求這k個數是嚴格遞增的,並且這k個數的g ...
- IT兄弟连 JavaWeb教程 JSP中的注释
由于JSP页面由HTML.JSP.Java脚本等组成,所以在其中可以使用多种注释格式 HTML中的注释 HTML语言的注释不会被显示在网页中,但是在浏览器中选择查看网页源代码时,还是能够看到注释的信息 ...