大神求助 django如何把静态文件中的图片全部显示到前端

qq_40998078 2018-03-06 09:48:41
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>display</title>
</head>
<body>
<img src="/static/images/0.jpg">
</body>
</html>


只会显示一张的
...全文
949 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
混沌鳄鱼 2018-03-06
  • 打赏
  • 举报
回复 2
没有试,只是凭感觉写的。

# views.py
import os
from django.conf import settings
from django.shortcuts import render

def list_all_img(request):
    img_list = []
    for static_path in settings.STATICFILES_DIRS:    
        img_list.extend(os.listdir(os.path.join(static_path, 'images'))

    return render(request,'app_path/list_all_img.html', {'img_list': img_list}




# list_all_img.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>display</title>
</head>
<body>
{% for img in img_list: %}
    <br/><img src="/static/images/{{ img }}">
{% endfor %}
</body>
</html>

37,718

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧