码迷,mamicode.com
首页 > 其他好文 > 详细

django.for的使用

时间:2015-02-09 16:02:33      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:python   django.for   

#encoding=utf-8
#made by davidsu33
#2015-2-9

from django.template import Template,Context
from django.conf import settings

class Athlete:
	name = ''
	height= 0
	weight = 0
	
	def __init__(self, *arg):
		'''
		print('arg_couont=', len(arg))
		print('arg_type=', type(arg[0]))
		print('arg[0]=', arg[0])
		print('arg[0].len=', len(arg[0]))
		'''
		
		if type(arg[0]) == list and len(arg[0]) == 3:
			thelist = arg[0]
			self.name 		= thelist[0]
			self.height 	= thelist[1]
			self.weight 	= thelist[2]
		else :
			raise Exception('Invalid Argument!!!')

	def __str__(self):
		return 'name=%s, height=%d, weight=%d' % 		(self.name, self.height, self.weight)
		
if __name__ == '__main__':
	html = '''
	<html>
	<head>
	<title>for test for </title>
	</head>
	
	<body>
	<ul>
	{% for athlete in althlete_list %}
		<li>{{athlete.name}} ,{{athlete.height}}, {{athlete.weight}}</li>
	{% endfor %}
	</ul>
	</body>
	
	'''
	settings.configure()
	#name height weight
	source = [
	['zhangsan', 180, 100], 
	['lisi', 168, 60],
	['wangwu', 175, 90],
	]
	
	athletes = []
	for i in source:
		athletes.append(Athlete(i))
		
	for i in athletes:
		print(str(i))
		
	t = Template(html)
	c = Context({'althlete_list': athletes})
	r = t.render(c)
	
	print('r=', r);
	
	#save to disk file
	file_path = 'd:/test.html'
	f = open(file_path, 'w+')
	assert(not f.closed)
	f.write(r)
	f.close()
	
	
	
	
	
	

django.for的使用

标签:python   django.for   

原文地址:http://blog.csdn.net/davidsu33/article/details/43671263

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!