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

<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. 武汉科技大学ACM :1010: 零起点学算法12——求2个日期之间的天数

    Problem Description 水题 Input 输入2个日期,日期按照年月日,年月日之间用符号-隔开(题目包含多组数据) Output 求出这2个日期之间的天数(不包括自身),每组测试数据一 ...

  2. nginx_笔记分享_配置篇

    参考http://www.howtocn.org/nginx:directiveindexhttp://blog.s135.com/ nginx 配置文档为 nginx.conf 比如我的配置文档 / ...

  3. 在线支付接口之PHP支付宝接口开发

    支付接口一般是第三方提供的代收款.付款的平台,可以通过支付接口帮助企业或个人利用一切可以使用的支付方式.常见支付平台:支付宝.快钱.云网支付.财付通. 支付宝页面:订单页面.状态页面.返回页面.--- ...

  4. php 防止SQL注入函数

    function inject_check($sql_str) { return eregi('select|insert|and|or|update|delete|\'|\/\*|\*|\.\.\/ ...

  5. How to install ffmpeg,mp4box,mplayer,mencoder,flvtool2,ffmpeg-php on centos

    1. Enable RPM Fusion yum repository The CentOS rpm packages of ffmpeg, mplayer, mencoder and MP4Box ...

  6. 防止ajax非正常访问

    http://www.cnblogs.com/yagzh2000/archive/2013/06/09/3128042.html http://www.cnblogs.com/henw/archive ...

  7. Android开发程序获取GPS信息步骤

    1.获取LOCATION_SERVICE系统服务.2.创建Criteria对象,调用该对象的set方法设置查询条件.3.调用LocationManager.getBestProvider(Criter ...

  8. Qt全局热键(windows篇)(使用RegisterHotKey和句柄进行注册)

    转载:http://www.cuteqt.com/blog/?p=2088 Qt对于系统底层,一直没有很好的支持,例如串口并口通信,还有我们经常都会用到的全局热键,等等.既然Qt可能出于某种原因,不对 ...

  9. BZOJ3314: [Usaco2013 Nov]Crowded Cows

    3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 86  Solved: 61[Subm ...

  10. cf581D Three Logos

    Three companies decided to order a billboard with pictures of their logos. A billboard is a big squa ...