作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
{{ file }} {% endfor %} 在我的 vi-6ren">
在我的 .html 文件中,我有这段代码:
<ul>
{% for file in files %}
<li><a href="{% static 'notebooks/<**Part that I want to reference dynamically**>' %}">{{ file }}</a></li>
{% endfor %}
</ul>
在我的 views.py 文件中:
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})
还有我的 url 文件:
url(r'^ipython/list', views.ipythonlist, name='ipythonlist')
所以我想做的是在我的静态文件夹中的目录中获取所有 .html 文件(或任何类型的文件,pdf、csv 等)的列表。然后我使用模板标签将这些数据从 View 传递到我的 html 中。我想动态链接到相应的文件,但我不确定该怎么做。
我尝试了 {% static 'notebooks/{{file}}' %},但只是返回了一个错误。
不确定如何操作,希望得到帮助!
因此,文件链接的示例是 {% static 'notebooks/chapter9.pdf' %}。其中chapter9,可以是任意名称。
最佳答案
{% for file in files %}
{% with file_with_path='notebooks/'|add:file %}
<li><a href="{% static file_with_path %}">{{ file }}</a></li>
{% endwith %}
{% endfor %}
关于python - 超链接中的 Django 模板标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35004294/
我是一名优秀的程序员,十分优秀!