出于 Python、Django 和 Postgresql 入門課程的目的,我正在尋找以 Django 和 Python 作為工具和語言的 html 檔案中檢索表資料并以結構化方式顯示它的最佳方法。
uj5u.com熱心網友回復:
要在 html 檔案中顯示 django 資料,只需執行以下操作:
在視圖中:
def login_view(request):
user = User.objects.all() #Here I have retrieved data from user table, in django table means Model.
return render(request, 'login.html', {'user':user}) #Here user is context
在 html 檔案中:
在 html 檔案中顯示資料。這里我使用花括號來識別 html 檔案中的背景關系
<html>
<body>
{% for data in user %} # user is context from login view, it is used display data in html file.
{{data}}
{% endfor %}
</body>
</html>
為什么我在 html 檔案中使用花括號,因為它是 django 中的模板語法
uj5u.com熱心網友回復:
要在 Django 中將來自 Postgres 的資料顯示為 HTML,您可以使用多種方法,具體取決于您希望如何顯示表格。但是,這個庫是一個很好的開始方式: https ://django-tables2.readthedocs.io/en/latest/
它將自己完成大部分作業,您只需將 qeuryset 傳遞給組件,它將幫助您根據輸入呈現 HTMl 表。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/511352.html
上一篇:僅在WooCommerce中為未來即將到來的訂單號添加前綴
下一篇:非常具體的選擇(或不選擇)
