doniyor
doniyor

Reputation: 37876

how to get file relative path with pythons os - django python

I am getting all file names under directory dir:

files = os.listdir(dir)

and after rendering file names to template, i am showing them in template like this:

{% for each in files %}
<li>
  <a href="{{each}}" target="_blank">{{each}}</a>
<li>
{% endfor %}

I want that If i click on filename, the file should be opened in new window. but here the problem is that files = os.listdir(dir) returns only file names and not its relative path. how do i get the path also?

Upvotes: 0

Views: 269

Answers (1)

ThinkChaos
ThinkChaos

Reputation: 1853

Add a '/' to tell the browser to start from the site's root. Also, opening the link in a new window should be HTML:

<a href="/{{each}}" target="_blank">{{each}}</a>

Edit:

files = [os.path.join(dir, f) for f in files]

Upvotes: 1

Related Questions