Python Django Template variables
One, passing string variables
# Cat Blog/template/index.html
<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/>
<title>{{title}}</title>
<body>
</body>
# Cat blog/views.py
From django.shortcuts import Render_to_response
def index (req):
Return Render_to_response (' index.html ', {' title ': ' Loyu page ', ' User ': ' Loyu '})
Second, pass dictionary, list variable
# Cat Blog/template/index.html
<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/>
<title>{{title}}</title>
<body>
<p>hello: {{user.age}}</p>
<p>hello: {{user.sex}}</p>
<p>{{book_list.0}}</p>
<p>{{book_list.1}}</p>
<p>{{book_list.2}}</p>
<p>{{book_list.3}}</p>
</body>
# Cat blog/views.py
From django.shortcuts import Render_to_response
def index (req):
user = {' name ': ' Loyu ', ' age ': $, ' sex ': ' Male '}
Book_list = [' Python ', ' java ', ' php ', ' web ']
Return Render_to_response (' index.html ', {' title ': ' Loyu page ', ' user ': User, ' Book_list ': book_list})
Third, transfer class variables
# Cat Blog/template/index.html
<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/>
<title>{{title}}</title>
<body>
</body>
# Cat blog/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 (' Loyu ', ' Male ')
Return Render_to_response (' index.html ', {' title ': ' Loyu page ', ' User ': User})
This article is from "Meteor Yu" blog, please be sure to keep this source http://8789878.blog.51cto.com/8779878/1850742
Iv. Python Django Template variables