Issue
I use an image gallery app on my website. At present I drop image files in a directory, and write img html tags for each image manually. Is it possible to make django create a list of files in the directory automatically, and send the json output to the gallery app, so that I can make javascript to generate <img>
elements for each image files. Or, whenever gallery app is requested, can I directly make django to auto-generate <img>
elements for each of the files in a directory.
Solution
here's a bit of code for you:
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 %}
Answered By - Hoff
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.