Python django模板中的可变图像
我是Django的新手,我想知道实现这一目标的最佳方式是什么 我在静态文件夹中保存了大量(15K+以上)的图像,我希望将这些图像显示在网页上,每次3个 我的尝试: 我可以在模板中硬编码名称,如下所示:Python django模板中的可变图像,python,django,Python,Django,我是Django的新手,我想知道实现这一目标的最佳方式是什么 我在静态文件夹中保存了大量(15K+以上)的图像,我希望将这些图像显示在网页上,每次3个 我的尝试: 我可以在模板中硬编码名称,如下所示: <div id="imageholder"> <img src="{% static "images/im1.jpg" %}" /> <img src="{% static "images/im2.jpg" %}" /> <img
<div id="imageholder">
<img src="{% static "images/im1.jpg" %}" />
<img src="{% static "images/im2.jpg" %}" />
<img src="{% static "images/im3.jpg" %}" />
</div>
但这当然不是15k+图像的选项。我考虑使用jQuery来更改函数中的名称
例如:
<div id="imageholder">
</div>
<script>
names = ['im1','im2','im3']
for (i = 0; i < names.length; i++) {
html = `<img src="{% static "images/${names[i]}" %}" />`
$('#imageholder').append(html)
}
</script>
名称=['im1'、'im2'、'im3']
对于(i=0;i
我相信这是可行的,但它要求我列出所有我没有的名字。我想也许我可以使用
os.listdir()
,但我不知道如何将其放入网页。在您的视图中,将列表添加到上下文:
images = os.listdir("path/to/images") #Can use glob as well
context = {'images': images}
return render(request,'/path/to/template',context}
在模板中:
{% for image in images %}
{% with 'path/to/imagedir/'|add:image as imagePath %}
<img src='{% static imagePath %}' />
{% endwith %}
{% endfor %}
{% for img in images %}
<img src="{% static 'img' %}">
{% endfor %}
{%用于图像中的图像%}
{%with'path/to/imagedir/'|添加:图像作为imagePath%}
{%endwith%}
{%endfor%}
在视图中,将列表添加到上下文:
images = os.listdir("path/to/images") #Can use glob as well
context = {'images': images}
return render(request,'/path/to/template',context}
在模板中:
{% for image in images %}
{% with 'path/to/imagedir/'|add:image as imagePath %}
<img src='{% static imagePath %}' />
{% endwith %}
{% endfor %}
{% for img in images %}
<img src="{% static 'img' %}">
{% endfor %}
{%用于图像中的图像%}
{%with'path/to/imagedir/'|添加:图像作为imagePath%}
{%endwith%}
{%endfor%}
您可以在上下文中预生成图像/imN.jpg
字符串的iterable,并在模板中对其进行迭代
class MyView(TemplateView):
...
def get_context_data(self, **kwargs):
ctx = super(MyView, self).get_context_data(**kwargs)
ctx['names'] = ('images/im{}.jpg'.format(i) for i in range(100))
return ctx
...
{名称中的名称为%}
{%endfor%}
您可以在上下文中预生成图像/imN.jpg
字符串的iterable,并在模板中对其进行迭代
class MyView(TemplateView):
...
def get_context_data(self, **kwargs):
ctx = super(MyView, self).get_context_data(**kwargs)
ctx['names'] = ('images/im{}.jpg'.format(i) for i in range(100))
return ctx
...
{名称中的名称为%}
{%endfor%}
在要将图像发送到模板的特定视图功能中
import os
images = os.listdir('/path_to_static')
return render(request,'template.html',{'images':images})
在模板中:
{% for image in images %}
{% with 'path/to/imagedir/'|add:image as imagePath %}
<img src='{% static imagePath %}' />
{% endwith %}
{% endfor %}
{% for img in images %}
<img src="{% static 'img' %}">
{% endfor %}
{%用于图像中的img%}
{%endfor%}
在要将图像发送到模板的特定视图功能中
import os
images = os.listdir('/path_to_static')
return render(request,'template.html',{'images':images})
在模板中:
{% for image in images %}
{% with 'path/to/imagedir/'|add:image as imagePath %}
<img src='{% static imagePath %}' />
{% endwith %}
{% endfor %}
{% for img in images %}
<img src="{% static 'img' %}">
{% endfor %}
{%用于图像中的img%}
{%endfor%}