问题描述
这些表的代码应该是:
<!DOCTYPE html>
<body>
<h1>Fun Market</h1>
<table border="1">
<thead bgcolor= "red">
<th>Clean</th>
</thead>
<tbody>
<tr>
<td>Cleanex $ 01.00</td>
</tr>
<tr>
<td>to clean</td>
</tr>
<tr>
<td>Toothbrush $ 01.50</td>
</tr>
<tr>
<td>oral hygiene</td>
</tr>
</tbody>
</table>
<table border="1">
<thead bgcolor= "red">
<th>Kitchen</th>
</thead>
<tbody>
<tr>
<td>Spaguetti $ 01.00</td>
</tr>
<tr>
<td>Easy to cook</td>
</tr>
<tr>
<td>Rice $ 1.00</td>
</tr>
<tr>
<td>For all family</td>
</tr>
</tbody>
</table>
<table border="1">
<thead bgcolor= "red">
<th>House</th>
</thead>
<tbody>
<tr>
<td>Door $ 25.00</td>
</tr>
<tr>
<td>A resistant door</td>
</tr>
</tbody>
</table>
</body>
</html>
我所做的是:
def textFile():
products = [['Clean',{'number': '42565','name': 'Cleanex','price': '01.00','description': 'to clean'},{'number': '45217','name': 'Toothbrush','price': '01.50','description': 'oral hygiene'}],['Kitchen',{'number': '47851','name': 'Spaguetti','description': 'Easy to cook'},{'number': '5852','name': 'Rice','price': '1.00','description': 'For all family'}],['House',{'number': '78595','name': 'Door','price': '25.00','description': 'A resistant door'}]]
#If it helps,products has the following estructure: [[Category,dictionary1,dictionary2,...],[Category,...]
sending = """
<!DOCTYPE html>
<body>
<h3>Fun Market</h3>
<hr>"""
for number in products:
sending = sending + """<h1 >"""+number[0]+""" </h1>"""
for i in range(1,len(number)):
sending = sending + """<h3 >"""+number[i]['name']+" $ "+number[i]['price']+"""</h3>"""
sending = sending + """<h4>"""+number[i]['description']+"""</h4>"""
for i in range(4):
sending = sending + """<h1> </h1>"""
sendingAgain = """
</body>
</html>
"""
sending = sending + sendingAgain
text = open("Store.html","wb")
text.write(bytes(sending,'utf-8'))
text.close()
textFile()
但我得到以下信息:
问题在于它没有放入表格中,我想将其放入表格中,然后使用 CSS 添加样式。 我希望你能帮助我,在此先感谢!
解决方法
这将在不使用模板的情况下为表格创建 HTML。
def textFile():
products = [['Clean',{'number': '42565','name': 'Cleanex','price': '01.00','description': 'to clean'},{'number': '45217','name': 'Toothbrush','price': '01.50','description': 'oral hygiene'}],['Kitchen',{'number': '47851','name': 'Spaguetti','description': 'Easy to cook'},{'number': '5852','name': 'Rice','price': '1.00','description': 'For all family'}],['House',{'number': '78595','name': 'Door','price': '25.00','description': 'A resistant door'}]]
#If it helps,products has the following estructure: [[Category,dictionary1,dictionary2,...],[Category,...]
sending = """
<!DOCTYPE html>
<body>
<h3>Fun Market</h3>
<hr>"""
for number in products:
table = f'<table border="1"><thead bgcolor="red"><th>{number[0]}</th></thead>'
table = table +"<tbody>"
for i in range(1,len(number)):
trow = f"<tr><td>{number[i]['name']+' '+number[i]['price']}</td></tr>"
table += trow
trow = f"<tr><td>{number[i]['description']}</td></tr>"
table += trow
table += "</tbody></table>"
sending +=table
sendingAgain = """
</body>
</html>
"""
sending = sending + sendingAgain
text = open("Store.html","wb")
text.write(bytes(sending,'utf-8'))
text.close()
textFile()
,
这是我会做的:
在views.py中:
def myMarket(request):
products = [['Clean','description': 'A resistant door'}]]
products_header = [i[0] for i in products] # get the first items "Clean,Kitchen,House"
products_body = [i[1:] for i in products] # get the other items "The dicts"
products_header_and_body = dict(zip(products_header,products_body)) # Convert them into key pairs
context = {
'products': products_header_and_body,}
return render(request,'myMarket.html',context)
在 HTML 模板中:
<!DOCTYPE html>
<body>
<h1>Fun Market</h1>
<table border="1">
{% for k,v in products.items %}
<thead bgcolor= "red">
<th>{{k}}</th>
</thead>
{% for item in v %}
<tbody>
{% for s,t in item.items %}
<tr>
<td>{{s}}: <b style="color: blue">{{t}}</b></td>
</tr>
{% endfor %}
</tbody>
{% endfor %}
{% endfor %}
</body>
</html>
输出:
您可以对 HTML 模板中的表格进行更多 css 样式设置。但是遍历列表所需要做的就是将其转换为密钥对。