首页 > 其他 > 详细

django基础4--django模板变量的使用

时间:2014-02-27 21:27:41      阅读:580      评论:0      收藏:0      [点我收藏+]

第一步

views中创建变量

[root@localhost myblog_rocky]# vihomepage/views.py

bubuko.com,布布扣

代码如下

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

bubuko.com,布布扣

代码如下:

<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


本文出自 “Rocky___” 博客,请务必保留此出处http://zutianbiao.blog.51cto.com/1361296/1363883

django基础4--django模板变量的使用,布布扣,bubuko.com

django基础4--django模板变量的使用

原文:http://zutianbiao.blog.51cto.com/1361296/1363883

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!