模板变量用双大括号显示,如:

<title>page title: {{title}}</title>

一 模板中使用变量

继续前面的例子,修改 index.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>{{title}}</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta name="keywords" content="Django Template" />
<meta name="description" content="Django Template" />
</head>
<body>
<center>{{context}}</center>
</body>
</html>

修改 views.py:

from django.shortcuts import render_to_response

def index(req):
return render_to_response('index.html', {'title':'Django Template Variable Sample', 'context':'A Sample Of Variable In Django Template !'})

访问 http://127.0.0.1:8000/blog/index 可以看到效果。

二 模板中使用字典变量

修改 views.py:

from django.shortcuts import render_to_response

def index(req):
user = {'name':'eli', 'age':24, 'sex':'male'}
return render_to_response('index.html', {'title':'Django Sample', 'context':user})

http://127.0.0.1:8000/blog/index 内容展示如下:

{'age': 24, 'name': 'eli', 'sex': 'male'}

模板中可以直接使用字典的键,index.html 可以如下:

<body>
<center>{{context.name}}</center>
</body>

如果模板文件中变量太多,可以使用 locals() 函数返回当前作用域中的所有变量组成的字典,使代码保持整洁漂亮!

 def index(req):
var1 = getvar1()
var2 = getvar2()
render_to_response('index.htm', locals())

三 模板中使用类和列表

views.py:

from django.shortcuts import render_to_response

class Person(object):
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex def say(self):
return "This is " + self.name def index(req):
user = Person('eli', 24, 'male')
user_list = ['eli', 'lie', 'iel']
return render_to_response('index.html', {'title':'Django Sample', 'context':user, 'users':user_list})

index.html:

<body>
  <center>{{context.name}}</center>

  <center>{{context.say}}</center>
  <center>
    <li>{{users}}</li>
    <li>{{users.0}}</li>
  </center>
</body>

注意:

1 模板中调用对象的方法时,要求方法无参数,且必须有返回值。

2 模板中调用引用时,优先顺序是:字典、对象属性、对象方法、列表引用

django: template variable的更多相关文章

  1. Django.template框架 template context (非常详细)

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

  2. The Django template language 阅读批注

    The Django template language About this document This document explains the language syntax of the D ...

  3. django template

    一.模板基本元素 1.例子程序 1)urls.py中新增部分 from django.conf.urls import patterns, url, include urlpatterns = pat ...

  4. django: template using & debug

    模板的作用方法有如下三种: blog/views.py: from django.template import loader, Context, Template from django.http ...

  5. django: template - built-in tags

    本节介绍模板中的内置标签:if for 承上文,修改 views.py 如下: from django.shortcuts import render_to_response class Person ...

  6. Django Template模板

    Django Template 你可能已经注意到我们在例子视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python 代码之中. 下面我们来调用html views def ind ...

  7. Django Template(模板)

    一.模板组成 组成:HTML代码 + 逻辑控制代码 二.逻辑控制代码的组成 1.变量 语法格式 : {{ name }} # 使用双大括号来引用变量 1.Template和Context对象(不推荐使 ...

  8. django.template.exceptions.TemplateDoesNotExist: rest_framework/api.html

    django.template.exceptions.TemplateDoesNotExist: rest_framework/api.html setting文件中的 INSTALLED_APPS加 ...

  9. django.template.exceptions.TemplateDoesNotExist: login.html 错误处理

    登陆Login界面时候报错 Internal Server Error: /login/ Traceback (most recent call last): File , in inner resp ...

随机推荐

  1. 单点登录CAS使用记(五):cas-client不拦截静态资源以及无需登录的请求。

    一.问题在哪? 在配置cas-client中,有这么一段配置: <filter> <filter-name>CAS Filter</filter-name> < ...

  2. Spring4.0学习笔记(2) —— 自动装配

    Spring IOC 容器可以自动装配Bean,需要做的是在<bean>的autowire属性里指定自动装配的模式 1)byType 根据类型自动装配,若IOC 容器中有多个目标Bean类 ...

  3. 在RichTextBox控件中添加图片和文字

    public void SetText(RichTextBox rtb) { rtb.Text = "在RichTextBox控件中添加图片和文字" + Environment.N ...

  4. 准备开发一个基于canvas的图表库,记录一些东西(一)

    开源的图表库已经有很多了,这里从头写个自己的,主要还是 提高自己js的水平,增加复杂代码组织的经验 首先写一个画图的库,供以后画图表使用.经过2天的开发,算是能拿出点东西了,虽然功能还很弱,但是有了一 ...

  5. sqlalchemy--group_concat的使用

    今天,一个app客户端同事需要我服务器端提供一组数据,这组数据要按类分好,把整个结构都展示给他,他直接使用就可以了.数据大概如下面这种: 同事需要的结构大概就是类型1有多少,分别是什么;类型2有多少, ...

  6. python实现发工资脚本

    好开心啊,在旁边大神的帮助下,终于实现了发工资的python脚本,之前用shell写的老出错,刚才测试,发80个人工资详情,妥妥的,代码如下: from email.mime.multipart im ...

  7. GET异步 请求图片步骤

    - (IBAction)getImage:(id)sender { //1,准备URL NSString *str = @"http://e.hiphotos.baidu.com/image ...

  8. window.onscroll

    http://www.w3help.org/zh-cn/causes/SD9013 1.各浏览器对 document.document.body.document.documentElement 对象 ...

  9. 分布式文件系统 FastDFS Ceph

    分布式文件系统 FastDFS Cephhttp://www.oschina.net/p/fastdfshttp://www.oschina.net/p/ceph FastDFS 的 Go 客户端 f ...

  10. WPF 自己动手来做安装卸载程序

    原文:WPF 自己动手来做安装卸载程序 前言 说起安装程序,这也许是大家比较遗忘的部分,那么做C/S是小伙伴们,难道你们的程序真的不需要一个炫酷的安装程序么? 声明在先 本文旨在教大家以自己的方式实现 ...