Python全系列 教程
3567个小节阅读:5931.4k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
需求:通过Tornado返回以上模板内容:
数据是动态的
计算总金额
引入静态资源
xxxxxxxxxx
<section class="wrap" style="margin-top:20px;overflow:hidden;">
<table class="order_table">
<tr>
<th><input type="checkbox" /></th>
<th>产品</th>
<th>名称</th>
<th>属性</th>
<th>单价</th>
<th>数量</th>
<th>小计</th>
<th>操作</th>
</tr>
{% set total = 0 %}
{% for order in orders %}
<tr>
<td class="center"><input type="checkbox" /></td>
<td class="center"><a href="product.html"><img src="{{order.get('img')}}" style="width:50px;height:50px;" /></a>
</td>
<td><a href="product.html">{{ order.get('name') }}</a></td>
<td>
<p>{{ order.get('type')}}</p>
</td>
<td class="center"><span class="rmb_icon">{{ order.get('price')}}</span></td>
<td class="center">
<span>{{ order.get('num') }}</span>
</td>
<!-- <td class="center"><strong class="rmb_icon">{{order.get('price')*order.get('num')}}</strong></td> -->
<td class="center"><strong class="rmb_icon">{{count_price(order.get('price'),order.get('num'))}}</strong></td>
<td class="center">{% raw order.get('opts') %}</td> <!-- 后端传递了标签,可以通过 raw 方式解析-->
<div hidden>
{{ total = total + count_price(order.get('price'),order.get('num')) }}
</div>
</tr>
{% end %}
</table>
<div class="order_btm_btn">
<a href="index.html" class="link_btn_01 buy_btn" />继续购买</a>
<a href="order_confirm.html" class="link_btn_02 add_btn" />共计金额<strong class="rmb_icon">{{total}}</strong>立即结算</a>
</div>
</section>
xxxxxxxxxx
from tornado import web
from tornado import ioloop
# 用来处理请求,并响应结果
class IndexHandler(web.RequestHandler):
def count_price(self,price:int,num:int) -> int:
return price*num
def get(self):
orders = [
{
'id':1,
'name':'MacPro 2060',
'type':'32G',
'price':10000,
'num':1,
'img':'/static/img/goods.jpg',
'opts':'<a href="delelte?id=1">删除</a>'
},
{
'id':2,
'name':'HuaWei Mate 2060',
'type':'32G',
'price':8000,
'num':2,
'img':'/static/img/goods007.jpg',
'opts':'<a href="delelte?id=2">删除</a>'
},
{
'id':3,
'name':'Sony耳机',
'type':'立体混音',
'price':2000,
'num':1,
'img':'/static/img/goods008.jpg',
'opts':'<a href="delelte?id=3">删除</a>'
},
]
self.render('shop13.html',orders = orders, count_price = self.count_price)
if __name__ =='__main__':
import os
base_path = os.path.dirname(__file__)
settings = {
'template_path':os.path.join(base_path,'templates'),
'static_path':os.path.join(base_path,'static'),
'static_url_prefix':'/static/'
}
# 创建一个tornado应用
app = web.Application([
('/',IndexHandler),
],debug = True,**settings)
# 设置监听端口
app.listen(8000)
# 通过事件循环来监听访问的端口
ioloop.IOLoop.current().start()