I am generating a template for an image gallery page. My approach is as follows:
- Host the images from a sub directory of an images folder
- The image folder will be titled the same as the gallery title
- The view passes a list of filenames to the template
- The template loops through the list and creates img tags
So my view would be
def some_gallery(request):
#LOGIC TO GET A LIST OF FILENAMES
variables = RequestContext(request,{
'user' : request.user,
'title' : 'something',
'files' : fileList
})
return render_to_response('gallery_template.html',variables)
And the template
....
{% for file in files %}
<img src="/path/to/images/{{ title }}/{{ file }}">
{% endfor %}
....
The problem I am running into is that Django is putting up a 500 error when I try to use the os.listdir function. How can I get the file list that I need??
Problematic version of the view which is giving the 500 error
def some_gallery(request):
variables = RequestContext(request,{
'user' : request.user,
'title' : 'something',
'files' : os.listdir('/path/to/gallery')
})
return render_to_response('gallery_template.html',variables)
Also I should note that the file path does work, so if I go directly to the url, I get just the image as expected.
EDIT: Fixed the typos in code samples