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

第一步

views中创建变量

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

wKiom1MNsaLB_hISAAHzC2vJbYw718.jpg

代码如下

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

wKioL1MNsYjjlUD5AAGVkBvqCzo921.jpg

代码如下:

<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


你可能感兴趣的:(安装,django,模板变量)