In my .html file, I have this code:
<ul> {% for file in files %}<li><a href="{% static 'notebooks/<**Part that I want to reference dynamically**>' %}">{{ file }}</a></li> {% endfor %}</ul>
In my views.py file:
def ipythonlist(request): files = [] main_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'static/')) ipython_file_path = os.path.join(main_dir, 'notebooks/') for file in os.listdir(ipython_file_path): if file.endswith(".html"): files.append(file) return render(request, 'pages/ipythonlist.html', {'files': files})
And my url file:
url(r'^ipython/list', views.ipythonlist, name='ipythonlist')
So what I am trying to do is get a list of all the .html files (or any type of files for that matter, pdf, csv, etc.) within a directory in my static folder. Then I pass this data from the views into my html using template tags. I want to link to that corresponding file dynamically, but I am not sure how to do that.
I tried {% static 'notebooks/{{file}}' %}, but that just returned an error.
Not sure how to do this and would appreciate some help!
So an example of the link to a file would be {% static 'notebooks/chapter9.pdf' %}. Where chapter9, can be any name.