我想在每次有人进入我的美术馆或刷新时显示随机图像,并每30秒更换一次我的美术馆上的图像。我已经在Django中正确设置了所有内容。在JS中,我创建了一个jpeg文件列表。我想随机选一张照片。我不确定如何在JS中使用Django语法{% %},也不知道这是否可能。我找到了一些code,它的功能如下:
//Add your images, we'll set the path in the next step
var images = ['banner-1.jpg', 'banner-2.jpg', 'banner-3.jpg', 'banner-4.jpg];
//Build the img, then do a bit of maths to randomize load and append to a div. Add a touch off css to fade them badboys in all sexy like.
$('<img class=" class="fade-in" src="images/' + images[Math.floor(Math.random() * images.length)] + '">').appendTo('#banner-load');
</script>
但是,我希望使用Django语法或类似的语法,这样就不必指定图像的路径。尽管如此,我也在寻找其他解决方案,所以我实际上不需要使用Django语法。
另外,有没有办法把它和一个计时器联系起来,让它继续为我的美术馆显示随机的图片?
谢谢!
发布于 2021-07-07 04:19:55
在您的模板中,您可以使用static
模板标记“填充”路径部分:
{% load static %}
<img src="{% static 'banner-1.jpg' %}" alt="My image">
关于图像列表,您可以从视图context传递该列表
对于计时器,你需要javascript。
你可以在https://docs.djangoproject.com/en/3.2/howto/static-files/上阅读更多
https://stackoverflow.com/questions/68276428
复制相似问题