【发布时间】:2016-11-15 09:25:49
【问题描述】:
刚开始学习python django框架,我正在尝试在html页面中显示自定义查询结果。
def index(request):
with connection.cursor() as cursor:
cursor.execute("SELECT * FROM polls_post")
context ={
'all_posts':cursor.fetchall()
}
在我的html页面中
<ul>
{% for post in all_posts %}
<li>{{ post }}</li>
{% endfor %}
</ul>
但是它将值显示为元组,所以我怎样才能得到带有表列名的查询结果,比如我可以这样打印值
<ul>
{% for post in all_posts %}
<li>{{ post.title }}</li>
<li>{{ post.name }}</li>
{% endfor %}
</ul>
【问题讨论】:
-
如果您刚开始学习 Django,是什么让您认为最好的方法是运行原始 SQL 查询而不是使用文档齐全的模型层?
-
我需要知道这是可能的,仅此而已:)