我想实现类似
{% for file in fileList %}
{% if file isfile %}
<a href="{% url 'GA:download' file %}">{{file}}</a><br>
{% endif %}
{% endfor %}
还是应该只更改模板的上下文?
我的
views.py
看起来像这样def index(request):
fileList = os.listdir(settings.MEDIA_ROOT)
context = {'fileList':fileList}
return render(request, 'gallery/index.html', context)
最佳答案
你可以做
{% for file in fileList %}
{% if file.media %}
<a href="{% url 'GA:download' file %}">{{file}}</a><br>
{% endif %}
{% endfor %}
关于python - Django模板语言中是否有isfile()或isdir()的等效项?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60134430/