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()
	
	
	
	
	
	

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值