我一直在开发Django应用,以帮助我们清理一些旧的Tableau工作簿。它为用户提供了前端Web ui来决定保留哪些书和删除哪些书。我将通过SQL和Django处理很多此类事情。
这是我编写的SQL查询:
from django.db import connection
username = str("lastname, firstname")
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 query():
cursor = connection.cursor()
cursor.execute("SELECT Owner_Name,Owner_Email,Id,Name FROM tableau_books WHERE Owner_Name = %s", (username,))
results = dictfetchall(cursor)
for item in results:
print(item)
现在,此功能只是查询我想要的字段并以清晰的方式打印它们。 这是我称之为的视图:
def dashboard(request):
if request.method == 'POST' and 'run_script' in request.POST:
# import function to run
from main_app.queries import query
# call function
query()
return render(request, 'dashboard.html')
最后,这是我用来实际在html中运行代码的按钮:
<form method="post">
{% csrf_token %}
<button type="submit" name="run_script">Find Workbooks</button>
</form>
是否有一种简便的方法,而不是将结果打印到终端,而是将其作为表格输入到dashboard.html?