django: template variable
模板变量用双大括号显示,如:
<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的更多相关文章
- Django.template框架 template context (非常详细)
前面的章节我们看到如何在视图中返回HTML,但是HTML是硬编码在Python代码中的 这会导致几个问题: 1,显然,任何页面的改动会牵扯到Python代码的改动 网站的设计改动会比Python代码改 ...
- The Django template language 阅读批注
The Django template language About this document This document explains the language syntax of the D ...
- django template
一.模板基本元素 1.例子程序 1)urls.py中新增部分 from django.conf.urls import patterns, url, include urlpatterns = pat ...
- django: template using & debug
模板的作用方法有如下三种: blog/views.py: from django.template import loader, Context, Template from django.http ...
- django: template - built-in tags
本节介绍模板中的内置标签:if for 承上文,修改 views.py 如下: from django.shortcuts import render_to_response class Person ...
- Django Template模板
Django Template 你可能已经注意到我们在例子视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python 代码之中. 下面我们来调用html views def ind ...
- Django Template(模板)
一.模板组成 组成:HTML代码 + 逻辑控制代码 二.逻辑控制代码的组成 1.变量 语法格式 : {{ name }} # 使用双大括号来引用变量 1.Template和Context对象(不推荐使 ...
- django.template.exceptions.TemplateDoesNotExist: rest_framework/api.html
django.template.exceptions.TemplateDoesNotExist: rest_framework/api.html setting文件中的 INSTALLED_APPS加 ...
- django.template.exceptions.TemplateDoesNotExist: login.html 错误处理
登陆Login界面时候报错 Internal Server Error: /login/ Traceback (most recent call last): File , in inner resp ...
随机推荐
- CSS布局注意(纯属个人总结)
和CSS样式有关多用class,和后台数据有关多用id. 1.使用绝对定位时(偏移量如:top,left...),如果父类没有相对定位,使用绝对定位的元素是相对根元素(即<html>标签) ...
- IO流文件字符输入输出流,缓冲流
由于字节输入输出流在操纵Unicode字符时可能有乱码现象 于是就有了操作字符的输入输出流 Reader ,Writer和他们的子类FileReader,FileWrite(其实就是用来辅助构造的 W ...
- 添加view类图中的二级菜单
void CFafdsafasdfasfasView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message han ...
- seo初学
对前端而言,做网站采用扁平式结构:控制网页链接数量,不能太少,当然也不能太多:其次采用扁平的目录层次,不能超过3次:三:导航优化,最好是文字,如果是图片的话,alt和title必须添加. 面包屑导航: ...
- HTTPS 服务器搭建
利用NGINX搭建HTTPS服务器不是一件困难的事情,过程包括以下几步 第一步:利用OpenSSL制作证书 第二步:安装NGINX,configure中保证加入ngx_http_ssl_module. ...
- 图的建立——邻接表表示(C语言+VC6.0平台)
图是一种重要而且相对复杂的数据结构,在实际编程中非常有用.邻接表是图的主要表示形式之一,是一种链接表表示方法. #include<stdio.h> #include<stdlib.h ...
- 简单运用 activity 的 button 点击事件
今天我们要讲的主要是四大组件之一Activity Activity 在英文中是活动的意思.活动就是我们与用户进行交互的一个场所. activity 整个的活动流程是什么呢?我们用一个图来看下 当然今天 ...
- Swift—类型检查与转换-备
继承会发生在子类和父类之间,是一系列类的继承关系. 例如:Person是类层次结构中的根类,Student是Person的直接子类,Worker是Person的直接子类. 这个继承关系类的具体实现代码 ...
- IE6\ IE7、IE8\9\10和Firefox的hack方式
#test{color:red;color:red !important;/ Firefox.IE7支持 */_color:red; / IE6支持 */*color:red; / IE6.IE7支持 ...
- C语言整数按照二进制逆序,输出逆序后的整数值
问题来源,今天早上和一舍友吃早餐的时候谈到的一个问题,将一个整数按照二进制逆序,然后输出逆序后的数值. 我们知道数值在内存中都是以二进制的形式存放的,假如我们是32位机,每8位为一个字节,int型在3 ...