views:

101

answers:

4

Hi

I created a list in Python:

mylist=os.listdir("/User/Me/Folder")

now I have a list of files in a List.

What I would like to do is:

Take one file name after the other and add a URL to it:

/myurl/ + each item in mylist

And then I would like to write the result in a html template from Django.

So that it display all the images in that folder in a list of html

How can I achieve this?

Thanks

+3  A: 
{% for filename in listoffilenames %}
  /myurl/{{ filename }}
{% endfor %}
Ignacio Vazquez-Abrams
Thanks for the answer but that does not work for me. I writes in the html:/myurl/[item1,...., item20]../myurl/[item1,...., item20]As many times as there is items in the list I guess.What did I do wrong?
MacPython
You put the list in the loop instead of the iterator variable.
Ignacio Vazquez-Abrams
Sorry but what would be the iterator variable?
MacPython
In the example I give it's `filename`.
Ignacio Vazquez-Abrams
I am a bit confused now. In the views.py I have:def mydef(request): allimg= os.listdir("/Users/Me/Folder") allimg[:] = ["/site_media/Gallery/"+ x for x in allimg] return render_to_response('mytemplate.html', {'allimg': allimg})In my template: {% for i in allimg %} /site_media/Gallery/{{ allimg }}{% endfor %}And I get the same as before but now with /site_media/Gallery/[item 1-item n]
MacPython
Why are you printing `allimg` if the iterator variable is `i`? And why are you doing it in *both* the view *and* the template?
Ignacio Vazquez-Abrams
A: 
for i in range(len(myList)):
    myList[i] = url + myList[i]

Sorry, can't help about the django part.

taskinoor
that's awful. please delete this answer.
SilentGhost
@taskinoor Can `range` take lists as arguments now? I'm stuck in the stone age with 2.6, must be a 2.7 or 3.1 thing.
Nick T
frankly speaking, im only aware of python part, have no idea about django. this is one way that python list can be traversed. but if this is "highly" related to some special knowledge of django, then im completely misunderstood. so mod can delete this in that case.
taskinoor
o, thats a mistake :-). thanks nick. it should be len(myList).
taskinoor
The error you made has nothing to do with Django.
Ignacio Vazquez-Abrams
ya, i agree. and that's a real mistake. should be more cool when writing the reply.
taskinoor
+1  A: 

For contrast, if you feel like doing this in the view (even though there's no good reason to do so):

mylist[:] = [url + x for x in mylist]

If you don't need to modify the existing list then you can drop the [:].

Ignacio Vazquez-Abrams
I didn't know the x[:] = y idiom. Thanks! (Might be confusing for a beginner though)
Daniel Hepper
+4  A: 

Using list comprehensions, you can transform your original list, "mylist", into a list with the URL prefix like so:

urllist = ['/myurl/%s' % the_file for the_file in mylist]

Analysis:

a) the expression in the square brackets is the list comprehension. it says: iterate over each item in "mylist", temporarily calling the iterated item "the_file" and transform it using the subexpression: '/myurl/%s' % the_file

b) the transformation expression says, create a string where %s is replaced by the string represented by the value of "the_file"

Joubert Nel
cool thanks a million for the explanation. And the_file has to be called the the_file or can be called any name or has to do with mylist?
MacPython
@MacPython It can be called anything, but it has to be consistent: For example ` '/myurl/%s' % a_shoe for a_shoe in mylist`
Tom
I am so close I can smell it. in the {% for url in urllist %} I wrote<p><img src='{{ urllist }}' alt="cogbio"/></p>But now it writes the list in the variable.But I want to write 1 file after the other, as a list of images.How can I achieve thisThanks so much!!
MacPython
I also have another problem in the list he writes me:['/site_media/Gallery/Image,...]So for every link he adds .Any idea why that can be?
MacPython
Yeah ! Rooibos you just made my week. I now understand what Ignacio wrote in other words. For everybody who will get this wrong in the future:In the template write:{% for url in urllist %}{{ url }} !!!!!And not{{ urllist }}Thanks so much for that. You cant understand how much that helped me. So simple.
MacPython