创建模板对象
Template类在django.template模板中

// 用django-admin.py startproject 命令创建一个项目目录
django-admin.py startproject django_template

// 进入目录
cd django_template

// 启动交互界面
python manage.py shell

// 在命令行输入下面三条(不用输入>>>)
>>> from django.template import Template

>>> t = Template("My name is {{ name }}.")

>>> print t

背景变量的查找
>>> from django.template import Template, Context

>>> person = {'name': 'Sally', 'age': '43'}

>>> t = Template('{{ person.name }} is {{ person.age }} years old.')

>>> c = Context({'person': person})

>>> t.render(c)

'Sally is 43 years old.'

下例使用了一个自定义类:

>>> from django.template import Template, Context

>>> class Person(object):

... def __init__(self, first_name, last_name):

... self.first_name, self.last_name = first_name, last_name

>>> t = Template('Hello, {{ person.first_name }} {{ person.last_name }}.')

>>> c = Context({'person': Person('John', 'Smith')})

>>> t.render(c)

'Hello, John Smith.'

命令行界面很好用:

句点也可用于访问列表索引,例如:

>>> from django.template import Template, Context

>>> t = Template('Item 2 is {{ items.2 }}.')

>>> c = Context({'items': ['apples', 'bananas', 'carrots']})

>>> t.render(c)

'Item 2 is carrots.'

模板系统一个经典实例(html和python分离的):
项目目录:MyDjangoSite
https://github.com/liuqiuchen/django

修改settings.py,加上templates的路径

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            # 添加templates路径
            BASE_DIR+"/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',
            ],
        },
    },
]

新建view.py

from django.shortcuts import render_to_response
def user_info(request):
    name = 'zbw'
    age = 24
    #t = get_template('user_info.html')
    #html = t.render(Context(locals()))
    #return HttpResponse(html)
    return render_to_response('user_info.html', locals()) # 加locals()才能显示模板数据

添加url:
urls.py

from django.conf.urls import url
from django.contrib import admin
from MyDjangoSite.view import user_info

urlpatterns = [
    url(r'^u/$',user_info),
    url(r'^admin/', admin.site.urls),
]

模板文件:
templates/user_info.html

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>用户信息</title>
</head>
<body>
    <h3>用户信息:</h3>
    <p>姓名:{{name}}</p>
    <p>年龄:{{age}}</p>
</body>
</html>

Django模板系统的更多相关文章

  1. django模板系统基础

    模板系统基础Django模板是一个string文本,它用来分离一个文档的展现和数据 模板定义了placeholder和表示多种逻辑的tags来规定文档如何展现 通常模板用来输出HTML,但是Djang ...

  2. python MVC、MTV 框架介绍 Django 模板系统常用语法

    Django 框架简介一.MVC框架和MTV框架1.MVC 全名Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分.优势: 耦合性低 重用性高 生命 ...

  3. Django 模板系统

    Django模板系统 常用语法 {{}} 变量相关 {%%} 逻辑相关 变量 格式 {{ 变量名 }} 命名规则 包括任何字母数字以及下划线 ("_")的组合 变量名称中不能有空格 ...

  4. Python学习(二十八)—— Django模板系统

    转载自http://www.cnblogs.com/liwenzhou/p/7931828.html Django模板系统 官方文档 一.常用语法 只需要记两种特殊符号: {{  }}和 {% %} ...

  5. Django模板系统(非常详细)(后台数据如何展示在前台)

    前面的章节我们看到如何在视图中返回HTML,但是HTML是硬编码在Python代码中的这会导致几个问题:1,显然,任何页面的改动会牵扯到Python代码的改动网站的设计改动会比Python代码改动更频 ...

  6. (转)Django学习之 第四章:Django模板系统

    前面的章节我们看到如何在视图中返回HTML,但是HTML是硬编码在Python代码中的 这会导致几个问题: 1.显然,任何页面的改动会牵扯到Python代码的改动 网站的设计改动会比Python代码改 ...

  7. django模板系统(上)

    filters 过滤 default 替代作用 filesizeformat 格式化为人类可读 add 给变量加参数 lower 小写 upper 大写 title 标题 ljust 左对齐 rjus ...

  8. python终极篇 ---django 模板系统

                                                模板系统                                                . MV ...

  9. Django模板系统:Template

    一.模板常用语法 1.1 变量 符号:{{ }} 表示变量,在模板渲染的时候替换成值 使用方式:{{ 变量名 }}:变量名由字母数字和下划线组成 点(.)在模板语言中有特殊的含义,用来获取对象的相应属 ...

随机推荐

  1. C#枚举描述获取

    public static class EnumExtension    {        public static string GetDescription(this Enum value)   ...

  2. 解决 webx.ml 中The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path 问题

    在Eclipse 的项目  上,点击鼠标右键 选中 ,并 Finish 即可.

  3. Asp.Net MVC4入门指南(3):添加一个视图

    在本节中,您需要修改HelloWorldController类,从而使用视图模板文件,干净优雅的封装生成返回到客户端浏览器HTML的过程. 您将创建一个视图模板文件,其中使用了ASP.NET MVC ...

  4. XE3随笔5:Format与转义字符

    unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

  5. 尽量少用if else

    Michael Feathers是Object Mentor International公司的技术顾问.他的工作不仅是技术开发,他还参与对世界各地技术团队进行培训.指导等工作.他曾开发了将JUnit迁 ...

  6. 获取当前html标签自定义属性的值

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  7. <input type="file" />浏览时只显示指定文件类型

    <input type="file" />浏览时只显示指定文件类型 <input type="file" accept="appli ...

  8. (Python)序列

    本节将学习一些循环序列的方法已经序列的大小比较规则 1.循环序列的方法 如果我们想同时循环打印一个列表的index和value,我们可以用enumerate(list) 函数 >>> ...

  9. 基本套接字编程(1) -- tcp篇

    1. Socket简介 Socket是进程通讯的一种方式,即调用这个网络库的一些API函数实现分布在不同主机的相关进程之间的数据交换. 几个定义: (1)IP地址:即依照TCP/IP协议分配给本地主机 ...

  10. 黑马程序员——for循环的使用与理解

    Console.WriteLine("请输入要打印菱形的行数(不能是偶数)");---------------------- <a href="http://edu ...