django - 我想将 django QUERY 表示为 html 页面中的表格,表格标题与 django 字段名称相同。我该如何实施?
问题描述
我想找到一种在 HTML 表上表示通过 SQL 查询获得的记录集的方法。我查阅了之前的一篇文章,该文章谈到了与我的类似问题,但在那篇文章中提到了一个模型,但我无法使其适应我的需求。我想调整该帖子的解决方案,以便即使在我使用 SQL 查询时它也能正常工作。
有可能吗?帖子是这样的:
我想将 django 模型表示为 html 页面中的表格,表格标题与 django 字段名称相同。我该如何实施?
这是我的看法:
def posts_discussione(request,pk):
discussione = Discussione.objects.get(pk=pk)
posts = Post.objects.raw(f'SELECT * from forum_post where discussione_id = {pk} order by dataCreazione ASC')
context = {'posts':posts,'discussione':discussione}
return render(request,'posts-discussione.html',context)
这是我的 HTML 代码:
<table>
<thead>
<th>Autore</th>
<th>Data</th>
<th>Contenuto</th>
</thead>
<tbody>
{% for post in posts %}
<tr>
<td>{{post.autorePost}}</td>
<td>{{post.dataCreazione}}</td>
<td>{{post.contenuto}}</td>
{% endfor %}
</tbody>
</table>
解决方案
推荐阅读
- spring-boot - SpringBoot 执行器保持状态
- reactjs - TypeError: Cannot read properties of undefined (reading 'map') Trying to Read REST api and display
- tkinter - 如何使用 grid_columnconfigure Tkinter 为每一行配置不同的列?
- html - 基于对象值渲染时创建引导卡网格的问题
- flutter - 无法在 Flutter Web 中的“assets/dotenv.develop”加载资产
- web-services - WSDL URL 返回 404 'File Not Found' 错误
- shell - 解析 json 值并使用 jq 将它们转换为 csv
- python - 使用 Pandas Python 为数据框提供标题和样式列
- kubernetes - 当我在 telnet 提示符下键入命令时,我键入的字符不会显示在 telnet 提示符中
- node.js - 将一组项目传递给后端