2016-10-24 52 views

回答

0

這些問題在一定程度上修改的版本: Django: Auto-generating a list of files in a directory

(現金@Hoff爲接受的答案上述職位):

的代碼位:

views.py

import os 

def gallery(request): 
    path="C:\\somedirectory" # insert the path to your directory 
    img_list =os.listdir(path) 
    return render_to_response('gallery.html', {'images': img_list}) 

gallery.html

{% for image in images %} 
<img src='/static/{{image}}' /> 
{% endfor %} 
0

這是Fakkabir的答案的修改,假設你正在嘗試加載圖像是通過提供/靜態/

views.py

from django.shortcuts import render 
import os 

def gallery(request): 
    path="C:\\somedirectory" # insert the path to your directory 
    img_list =os.listdir(path) 
    return render(request, 'gallery.html', {'images': img_list}) 

gallery.html

{% load static %} 

{% for image in images %} 
    <img src= '{% static image %}'> 
{% endfor %} 
相關問題