Python 在html表中模板化直接原始查询
我是django的初学者,我使用的是只读数据库,我只想做一些选择,并在我的模板中将其显示为一个表,但我无法将coulmn按列返回到我的html表中,请帮助我, 我使用的是directky原始查询 Model.py 从django.db导入连接Python 在html表中模板化直接原始查询,python,html,django,django-templates,Python,Html,Django,Django Templates,我是django的初学者,我使用的是只读数据库,我只想做一些选择,并在我的模板中将其显示为一个表,但我无法将coulmn按列返回到我的html表中,请帮助我, 我使用的是directky原始查询 Model.py 从django.db导入连接 # Create your models here. def dictfetchall(cursor): "Returns all rows from a cursor as a dict" desc = cursor.descriptio
# Create your models here.
def dictfetchall(cursor):
"Returns all rows from a cursor as a dict"
desc = cursor.description
return [
dict(zip([col[0] for col in desc], row))
for row in cursor.fetchall()
]
def my_custom_sql(self):
with connection.cursor()as cursor:
cursor.execute("""
SELECT EQUIP_ID, LINE_CODE, PLANT_CODE
FROM tbs_rm_mnt_shift_sumr
where SUMR_YMD = '20180405' AND SIDE_CODE = 'T' AND
rownum < 20
""" )
row = dictfetchall(cursor)
return row
monitoring.html
<table border="2" style="solid black">
<tr>
<td>Equip</td>
<td>Line</td>
<td>Plant</td>
{% for instance in query %}
{% for field, value in instance.items %}
<tr>
<td>{{ value }} </td>
</tr>
{% endfor %}
{% endfor %}
</tr>
</table>
装备
线
种植
{查询%中的实例为%}
{%为字段,值在instance.items%}
{{value}}
{%endfor%}
{%endfor%}
浏览器输出:
目前,您仅为每行输出一个
td
{% for instance in query %}
{% for field, value in instance.items %}
<tr>
<td>{{ value }} </td>
</tr>
{% endfor %}
{% endfor %}
但是,您不能假定字典中键的顺序,因此您应该按键访问项目,或者重新考虑是否要使用dictfetchall
{% for instance in query %}
<tr>
<td>{{ instance.EQUIP_ID }} </td>
<td>{{ instance.LINE_ID }} </td>
<td>{{ instance.PLANT_CODE }} </td>
{% endfor %}
</tr>
{% endfor %}
{%例如查询%中的实例]
{{instance.equipment_ID}
{{instance.LINE_ID}
{{instance.PLANT_CODE}
{%endfor%}
{%endfor%}
谢谢,这确实解决了我的问题,
{% for instance in query %}
<tr>
{% for field, value in instance.items %}
<td>{{ value }} </td>
{% endfor %}
</tr>
{% endfor %}
{% for instance in query %}
<tr>
<td>{{ instance.EQUIP_ID }} </td>
<td>{{ instance.LINE_ID }} </td>
<td>{{ instance.PLANT_CODE }} </td>
{% endfor %}
</tr>
{% endfor %}