Python目录列表返回到Django模板 [英] Python directory list returned to Django template

查看:107
本文介绍了Python目录列表返回到Django模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处共有Python newb.我有一个images目录,我需要将这些文件的名称和url返回到django模板,我可以循环访问该模板以获取链接.我知道它将是服务器路径,但是我可以通过JS对其进行修改.我已经尝试过os.walk,但结果一直是空的.

Total Python newb here. I have a images directory and I need to return the names and urls of those files to a django template that I can loop through for links. I know it will be the server path, but I can modify it via JS. I've tried os.walk, but I keep getting empty results.

推荐答案

如果图像位于一个目录中

If your images are in one directory

import os
root="/my"
Path=os.path.join(root,"path","images")
os.chdir(Path)
for files in os.listdir("."):
    if files[-3:].lower() in ["gif","png","jpg","bmp"] :
         print "image file: ",files

这篇关于Python目录列表返回到Django模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆