上一次提到通过调用jobs.views中的index 方法,返回了一个页面,相关代码如下:
from django.http import HttpResponse
def index(req):
return HttpResponse('<h1>Hello world!</h1>')
但实际应用中,我们不可能通过这样的方法来展现网页,而是需要将这门语言嵌套到HTML 的模板或者说框架中来展示。接下来就简单的实现一下将django语言嵌套到html 模板中。要实现上述状况有2种方法:
一、创建静态页面
方法一:
通过template 加载模板,生成Context对象,存放模板所需数据,通过模板对象对数据进行渲染,然后通过HttpResponse 输出。
1、生成HTML 模板
可以通过Dreamweaver 生成以下模板
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Django Page</title>
</head>
<body>
<h1>Hello Django</h1>
</body>
</html>
模板文件都放在应用程序下面的templates 目录,这个目录默认不存在需要手动创建
[root@node1 webproject]# mkdir jobs/templates
创建模板文件,将上述HTML 代码贴入
[root@node1 webproject]# vim jobs/templates/index.html
2、修改views.py
[root@node1 webproject]# vim jobs/views.py
from django.http import HttpResponse
from django.template import loader,Context // 导入django 的两个对象loader和Context
def index(req):
t = loader.get_template('index.html') // 导入模板文件jobs/templates/index.html
c = Context({}) // 创建Context 对象,用于存放提供给模板的数据(用于动态网页)
return HttpResponse(t.render(c)
3、启动服务测试
[root@node1 webproject]# python manage.py runserver 0.0.0.0:8001
方法二:
1、修改views.py 文件
[root@node1 webproject]# vim jobs/views.py
from django.shortcuts import render_to_response
def index(req):
return render_to_response('index.html', {}) // 第一个参数是模板文件,第二个参数是Context值
2、测试
可以看到能达到同样的效果
二、创建动态页面
通过模板变量,将数据动态的传递给HTML 文件并输出,而不是写死在HTML 页面里面
1、修改模板文件(下面标红的便为模板变量)
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> `title` </title>
</head>
<body>
<h1>hello `user` </h1>
</body>
</html>
2、修改views.py 文件
(1)普通字符串
[root@node1 webproject]# vim jobs/views.py
from django.shortcuts import render_to_response
def index(req):
return render_to_response('index.html', {'title':'My first page', 'user':'pmghong'})
从上面可以看到,通过Context对象将title和user 这两个变量的值传递给了index.html
(2)字典
修改index.html
<body>
<h1>hello `user`.`name`</h1>
<h1>Age: `user`.`age`</h1>
<h1>Sex: `user`.`sex`</h1>
</body>
[root@node1 webproject]# vim jobs/views.py
from django.shortcuts import render_to_response
user = {'name':'pmghong', 'age':23, 'sex':'male'}
def index(req):
return render_to_response('index.html', {'title':'My first page', 'user':user})
(3)对象
[root@node1 webproject]# vim jobs/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 index(req):
user = Person('Tom', 23, 'male')
return render_to_response('index.html', {'title':'My first page', 'user':user})
(4)列表
[root@node1 webproject]# vim jobs/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 index(req):
user = Person('Tom', 23, 'male')
book_list = ['python', 'django', 'perl', 'php']
return render_to_response('index.html', {'title':'My first page', 'user':user, 'book_list':book_list})
修改index.html,加入下面几行:
[root@node1 webproject]# vim jobs/templates/index.html
<h1>The language you like :</h1>
<h1>`book_list`.`0`</h1>
<h1>`book_list`.`1`</h1>
<h1>`book_list`.`2`</h1>
<h1>`book_list`.`3`</h1>
(5)类的方法
[root@node1 webproject]# vim jobs/views.py
在上面的Person 类中添加say的方法
def say(self):
return "I'm " + self.name
修改index.html,加入下面一行:
[root@node1 webproject]# vim jobs/templates/index.html
<h1>Mr.`user`.`name` say : `user`.`say`</h1>
总结
从上面的输出可以看到,Context 可以传递普通变量、字典、对象、方法、列表等,他们之间存在着以下的优先级:
普通变量 > 字典 > 对象的属性 > 对象的方法 > 列表