如何使用flask(使用引导程序)和python列出目录中的文件?
这是我在这个论坛上提出的第一个问题。我非常渴望找到解决方案...希望我能向你们学习。 我有一个解决方案,但是在浏览器窗口中发布整个目录树并知道文件名,这不是我想要的。
现在我找到了这样的代码片段并对其进行了一些修改。它的生成器表达式,我认为 Jinja2 不支持:
import os
path = r"C:/Users/add706/Documents/NRL_webseite/website/static/uploads"
def get_files(path):
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)):
yield file
for file in get_files(path):
print(file)
输出在路径中给出了 3 个文件: 1.jpeg postgre.jpeg winrar-x64-610d.exe
我正在尝试制作一个列表并将其传递给 JINJA2。我已经创建了一个模板,但不知何故,当我运行 Flask 时,我无法列出文件并且打印功能为空。三天以来,我一直坐在它上面,甚至没有错误消息,这对我有帮助。
这是我对 auth.py 的原始截取(库的导入很好,此处未列出):
path = r"C:/Users/add706/Documents/NRL_webseite/website/static/uploads"
@auth.route('/', methods = ['GET', 'POST'])
def get_files(path):
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)):
return (os.listdir(path))
files=[]
for file in get_files(path):
files.append(file)
print(files)
return render_template('home.html', files=get_files(path))
这是我对 home.html 模板的原始截取(它是 %extended%),循环遍历返回的文件(我希望.. .):
<!-- download Folder-->
<div align="center">
<image src="{{ url_for('static', filename='uploads/postgre.jpeg')}}">
</ul>
{% for file in files %}
<li class="collection-item"><a href="#">{{ file }}</a></li>
{% endfor %}
</ul>
</div>
现在的问题是:我必须在两个文件中更改什么才能在本地网页上看到这 3 个文件 (http://127.0.0.1:5000/)?我想列出它们并使它们可单击,因此在单击时,可以将文件作为附件下载。第二件事是列出它们的上传时间。第三件事是,下载过程中会弹出一个窗口,询问我“您想下载(文件名)吗?为了形象化我的问题,我上传了一张图像并画了红色框。感谢您提供的每一个帮助.
图片链接:(https://i.sstatic.net/pFKrP.jpg
)已经找到这篇文章,在我的脚本中说树未定义(用flask列出目录中的文件< /a>),所以我放弃了。
This is my first Question I make on this forum. I am so desperate to find a solution... Hope I can learn from you guys.
I had a solution, but with posting the whole directorytree in the browserwindow and knowing the filename, which is not like I want it.
Now I found such code snippet and changed it a little bit. Its generator expression, which is I think not supported with Jinja2:
import os
path = r"C:/Users/add706/Documents/NRL_webseite/website/static/uploads"
def get_files(path):
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)):
yield file
for file in get_files(path):
print(file)
The output gives me 3 files inside the path:
1.jpeg
postgre.jpeg
winrar-x64-610d.exe
I am trying to make a list and pass it to JINJA2. I have created already a template, but somehow, when I run flask, I cant list the files and the print function is empty. Since 3 days I am sitting on it and there is not even an error message, which could help me.
This is my original outtake of auth.py (imports of libraries are fine, not listed here):
path = r"C:/Users/add706/Documents/NRL_webseite/website/static/uploads"
@auth.route('/', methods = ['GET', 'POST'])
def get_files(path):
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)):
return (os.listdir(path))
files=[]
for file in get_files(path):
files.append(file)
print(files)
return render_template('home.html', files=get_files(path))
This is my original outtake of home.html template (which was %extended%), looping through the returned files ( I wish to...):
<!-- download Folder-->
<div align="center">
<image src="{{ url_for('static', filename='uploads/postgre.jpeg')}}">
</ul>
{% for file in files %}
<li class="collection-item"><a href="#">{{ file }}</a></li>
{% endfor %}
</ul>
</div>
The question is now: What do I have to change in my both files that I can see the 3 files on my local webpage (http://127.0.0.1:5000/)? I would like to list them and make them clickable, so while clicking, the file can be downloaded as attachment. Second thing is, to list them with upload time. THird thing is, that the download process, let a window popping up, which asks me "DO you wish to download (filename). To visualize my problem, I uploaded an image and drew red boxes. Thanks in regard for every help out there.
Image link: (https://i.sstatic.net/pFKrP.jpg)
I have found this article, which says in my script that tree is undefined (List files in directories with flask), so I gave it up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你确实可以在 jinja2 中使用生成器。
我写的例子应该可以满足你的需求。
它将目录中的文件显示为列表。每个条目都有其大小和上传时间。 uploads 文件夹位于 实例文件夹 中,以分隔来自应用程序的文件。
我使用 flask-moment 显示正确的时间。它使用 moment.js 并显示客户端各自时区的时间戳。
我使用自定义 Jinja2 过滤器 来显示文件大小.
如果单击文件进行下载,则会打开一个对话框,要求确认。这是使用 JavaScript 完成的。
享受实现目标的乐趣。
Flask (app.py)
HTML (templates/index.html)
我已将我的代码添加到您的项目中。现在应该可以了。
您应该致力于构建您的项目,以便您可以更轻松地找到解决代码的方法。在这种情况下,我无法免除你的这项任务。
(网站/init.py)
(网站/auth.py)
(网站/views.py)
(templates/base.html)
(templates/home.html)
You can indeed use a generator inside jinja2.
The example I wrote should meet your needs.
It displays the files within the directory as a list. Each entry is assigned its size and the time of upload. The uploads folder is inside the instance folder to separate the files from the application.
I use flask-moment to show the correct times. This uses moment.js and displays the timestamp in the respective time zone of the client.
I use a custom Jinja2 filter to display the file size.
If a file is clicked to download, a dialog opens that requests confirmation. This is done using JavaScript.
Have fun achieving your goals.
Flask (app.py)
HTML (templates/index.html)
I've added my code to your project. It should work now.
You should work on structuring your project so that you can find your way around the code more easily. I cannot relieve you of this task in this context.
(website/init.py)
(website/auth.py)
(website/views.py)
(templates/base.html)
(templates/home.html)
在 @ Detlef 的帮助下,创建了一个本地网络服务器,它允许您登录、注册和留下笔记。然后设置了上传和下载选项。
我从树结构开始。
从main.py开始:
这是auth.py:
这是views.py:
这是init.py:
这是models.py:
这是home.html模板:
这是login.hmtl
这是sign_up.hmtl
这是base.hmtl
玩得开心。感谢@Detlef!!!!!!!
With the help of @ Detlef a local webserver was created, which allows you to log in, sign up and leave notes. Then there were an upload and a download option set up.
I begin with the tree structure.
Starting with the main.py:
This is the auth.py:
This is the views.py:
This is the init.py:
This is the models.py:
THis is the home.html template:
This is the login.hmtl
This is the sign_up.hmtl
This is the base.hmtl
Have fun with it. AND THANKS to @ Detlef !!!!!!!