第一步
在views中创建变量
[root@localhost myblog_rocky]# vihomepage/views.py
代码如下
from django.shortcuts importrender_to_response
user =("name":"rocky","age":"22","constellation
user =("name":"rocky","age":"22","constellation":"Taurus
#from django.http import HttpResponse
from django.shortcuts importrender_to_response
user =("name":"rocky","age":"22","constellation":"Taurus","QQ":"920862715")
def index(req):
# return HttpResponse('<h1>hello welcome to Rocky\'sblog!</h1>')
return render_to_response('index.html',{"title":"hellowelcome to Rocky\'s blog!","user":user})
第二步
在模板中引用变量
vi homepage/templates/index.html
代码如下:
<li>constellation:`user`.`constellation`</li>
<HTML>
<HEAD>
<META NAME="GENERATOR"Content="Microsoft Developer Studio">
<META HTTP-EQUIV="Content-Type"content="text/html; charset=gb_2312-80">
<TITLE>Rocky's blog</TITLE>
</HEAD>
<BODY>
<h1>`title`</h1>
<li>name: `user`.`name`</li>
<li>age: `user`.`age`</li>
<li>constellation:`user`.`constellation`</li>
<li>QQ: `user`.`QQ`</li>
</BODY>
</HTML>
第三步
测试
python manage.py runserver
转载于:https://blog.51cto.com/zutianbiao/1363883