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

<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. log4j的配置及使用

    用日志的好处: 可以长久的保存日志信息. 日志可以保存到:网络.文件.数据库 设置日志的级别. OFF Fatal – System.exit(0); - JVM, ERROR – 错误,模块错误. ...

  2. VS2013 编译 MySql Connector C 6.1.6

    1.下载cmake http://cmake.org/ 2.下载最新版MySql Connector C http://www.mysql.com 3.命令行下,转到源代码目录下,"cmak ...

  3. mysql导入excel数据

    1.第一步我们得到了一个excel表,里面有很多需要我们导入的数据. 2.删除第1行"准考证号""XXX"....只保留我们需要的数据部分. 3.单击" ...

  4. Python3 如何优雅地使用正则表达式(详解五)

    非捕获组命名组 精心设计的正则表达式可能会划分很多组,这些组不仅可以匹配相关的子串,还能够对正则表达式本身进行分组和结构化.在复杂的正则表达式中,由于有太多的组,因此通过组的序号来跟踪和使用会变得困难 ...

  5. 怎么用visual studio2010编写c++程序|用visual studio2010编写c++程序的步骤

    如何通过visual studio 2010编写一个简单的c++程序,随小编不一起看看如何编写. 首先打开visual studio 2010 点击软件左上角“文件-新建-项目”,选择“win32-w ...

  6. Hive笔记--sql语法详解及JavaAPI

    Hive SQL 语法详解:http://blog.csdn.net/hguisu/article/details/7256833Hive SQL 学习笔记(常用):http://blog.sina. ...

  7. 汇编下的i++与++i

    故事背景,一个正在c语言的家伙,问我++i 和 i++的问题,我当时因为要去上课没给他说,正好今晚有空就测试了一下如下代码: 编译环境:VS2010  语言:C++ #include <iost ...

  8. (摘自ItPub)物理standby中switchover时switchover pending的解决办法

    http://www.itpub.net/thread-1782245-1-1.html DataGuard一主一物理备,sid为primary和standby,现在要把primary切换成备库,st ...

  9. weblogic使用脚本部署

    --本机 (/common/bin/wlst.sh (2)connect('weblogic','weblogic1','t3://localhost:7001') (3)progress=deplo ...

  10. Qt入门(4)——Qt常见控件

    Qt提供了大量的内建控件及通用对话框可满足程序员的绝大部分要求.我们将对这些控件和对话框作一个大概的介绍. 1. QLabel 定义 QLabel* m_labelOrdered = newQLabe ...