【发布时间】:2022-03-19 04:31:12
【问题描述】:
我正在使用 os.listdir 获取子目录列表,并希望将它们显示为网页上的表格。我正在为 web 框架使用瓶子。我在瓶子文档中看到,可以通过从列表中创建一个 sqlite 数据库并从那里加载它来完成,但理想情况下我正在寻找一种避免该中间步骤的方法。如果没有 sqlite 步骤,我找不到任何文档,想知道是否可能?
下面的当前代码在网页上显示我想要的信息,但显示为单行文本。
当前代码:
瓶子应用
from bottle import Bottle, template, route, run, static_file
import os
import sys
app = Bottle()
dirname = os.path.dirname(sys.argv[0])
@app.route('/static/<filename:re:.*\.css>')
def send_css(filename):
return static_file(filename, root=dirname+'/static/')
@app.route('/')
def index():
dir_loc = "/Users/me/Documents"
dir_data = list(os.listdir(dir_loc))
return template('index', data = dir_data)
run(app, host='localhost', port=8080, debug=True)
index.tpl
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="static/styling.css">
</head>
<body>
<div>
<p>Template for bottle showing {{data}}</p>
</div>
</body>
</html>
【问题讨论】:
-
您不需要(或不想要)一个数据库。查看模板中的循环 -
for循环似乎合适。