views:

46

answers:

1

Since my last question here: http://stackoverflow.com/questions/3166221/python-images-display

I understood that from all the answers I got the glob.glob could be the only one in the direction I need.

However where I am stuck right now is here:

I can create a list with all the filenames in my media directory by using glob.glob:

all = glob.glob("/Path_to_MEDIA/*/*.jpg")

But how can I use that and create a VERY SIMPLE image display with one next button that calls files in my MEDIA_ROOT and displays them.

What I know is:

  1. I have a Template which looks something like the default directory index:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
      <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <meta http-equiv="Content-Language" content="en-us" />
        <meta name="robots" content="NONE,NOARCHIVE" />
        <title>Index of {{ directory }}</title>
      </head>
      <body>
        <h1>Index of {{ directory }}</h1>
        <ul>
          {% ifnotequal directory "/" %}
          <li><a href="../">../</a></li>
          {% endifnotequal %}
          {% for f in file_list %}
          <li><a href="{{ f|urlencode }}">{{ f }}</a></li>
          {% endfor %}
        </ul>
      </body>
    </html>
    
  2. I need to create a def in my views that feeds the list from glob.glob to this or similar template.

What I dont know:

  • How does this def in the view have to look like?

And here:

  • What do I have to write to display one image, sound in a browser?
  • What do I have to write to display a LIST of images, sounds?

Thanks for the time!

A: 

Make a direct-to-template url with extra-context in urls.py:

from django.views.generic.simple import direct_to_template
...
url(r'^whatever', direct_to_template, 
                 { 'template':'foo.html', 'extra_context': {'files':myfiles} }
                 name='whatever' ),

Where myfiles above is a list/tuple of your files. However, make sure to format your file list in terms of MEDIA_URL instead of based on MEDIA_PATH. For example:

myfiles = [ 'relative/path/foo.jpg', 
            'http://static.mysite.com/absolute/path/bar.jpg' ]

Though, obviously generated from the filesystem in your case, not a hardcoded list. And you could do the work in a view rather than using direct-to-template -- just make sure to put the files key/value into your context:

def myview( request ... ):
  context = RequestContext(request)
  context[files]=myfiles
  return render_to_respone( ..., context_instance=context )

Then, in your template foo.html:

{% for file in files %}
  <img src='YOUR_MEDIA_URL_HERE/{{ file }}' />
{% endfor %}
eruciform
Thanks! Interesting. And where is myfiles specified? In a Model.py, views.py , def in a View, model?
MacPython
the top block is in urls.py, so myfiles can be filled from there as well. i do this for a blog, where the "top stories" are calculated there, since they're shared between multiple views. however, you can put it else where and call it there. or, if you like, you can write your own view instead of doing the generic view thing, and just make sure to add the files key/value to your context.
eruciform