我想创建一个HTML表来显示一个mongodb集合的内容。该集合包含有关来自小企业的不同客户订单的数据。某些数据将始终存在于给定的文档中,例如客户名称和电话号码。但是,每个文件中的一些数据片段需要变化,例如订购的物品,因为一个客户可能订购1件物品,而另一个可能订购3件物品。因此,如果我在每个文档中都有一个包含任意数字字段的文档的MongoDB集合,我如何动态地将它们添加到HTML表格中以显示文档的内容?作为我正在寻找的显示器类型的示例,这里是我知道的字段的硬编码HTML将保持不变。用HTML显示MongoDB文档
<!DOCTYPE html>
<html>
<head>
<head>
<title>Invoice Report</title>
<style type="text/css">
body {font-family:sans-serif;color:#4f494f;}
form input {border-radius: 7.5px;}
h5 {display: inline;}
.label {text-align: right}
.ordersBook {float:left; padding-top: 10px;}
.name {width:100%;float:left; padding:3px;}
.wrapper { padding-left: 25px; padding-top: 20px}
</style>
<script type="text/javascript">
var itemRe = /item*/;
}
</script>
</head>
</head>
<body>
<div class="invoice">
<h4>Order Form:</h4>
<table border="1">
<tr>
<th>Name:</th>
<td>{{rows['name']}}</td>
</tr>
<tr>
<th>Created:</th>
<td>{{rows['created']}}</td>
</tr>
<tr>
<th>Phone:</th>
<td>{{rows['phone']}}</td>
</tr>
<tr>
<th>Email:</th>
<td>{{rows['email']}}</td>
</tr>
<tr>
<th>Item:</th>
<td>{{rows['item']}}</td>
</tr>
</div>
<tr>
<th>Quantity:</th>
<td>{{rows['qty']}}</td>
</tr>
<tr>
<th>Color:</th>
<td>{{rows['color']}}</td>
</tr>
<tr>
<th>Quote:</th>
<td>{{rows['quote']}}</td>
</tr>
</table>
</div>
</body>
</html>
它可能是更好的动态创建整个表,但我不知道在哪里合适的地方做,这是
- 在HTML文件中的JavaScript函数?
- 或者也许在pongongo文件中保存mongodb数据库中的信息?
处理将mongodb文档发送到HTML表单的python代码使用python Bottle模板。
@bottle.route('/view/<_id>', method = 'GET')
def show_invoice(_id):
client = pymongo.MongoClient("mongodb://localhost")
db = client.orders
collection = db.myorders
from bson.objectid import ObjectId
result = collection.find_one({'_id': ObjectId(_id)})
return bottle.template('invoice', rows = result)
我非常感谢有人可以提供任何帮助! =)
非常感谢! – sreisman