django - 为什么引导轮播和 django 不能正常工作?
问题描述
我正在尝试使用 django 提供引导轮播后端。我编写了一个简单的程序视图,它将所有图像传递给模板。但是当我运行程序时,我没有按预期得到输出,所有图像都被渲染为彼此相邻。没有什么像前进和后退按钮之类的了。我正在发布我的视图和模板。
提前致谢。
希望很快从你这里。
看法:-
def TaggingView(request):
queryset = MyImage.objects.filter(is_tagged=False)
return render(request, "taggging.html", {'files': queryset})
模板 :-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div id="carouselInd" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
{% for file in files %}
{% with forloop.counter0 as i %}
<div class="carousel-item {% if i is 0 %}active{% endif %}">
<img class="d-block w-100" src="{{ file.image.url }}" alt="" style="height: 30rem;">
</div>
{% endwith %}
{% endfor %}
</div>
<a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</body>
</html>
输出:-
解决方案
Django 与 Bootstrap 轮播一起工作正常,你需要注意这样的活动类:
<div id="carouselInd" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
{% for file in files %}
{% with forloop.counter0 as i %}
<div class="carousel-item {% if i is 0 %}active{% endif %}">
<img class="d-block w-100" src="{{ file.image.url }}" alt="
{{ file.name }}" style="height: 30rem;">
</div>
{% endwith %}
{% endfor %}
</div>
</div>
注意:alt
属性是动态的,您可以根据需要使用静态值。并且给height
图像加上一个也可以帮助包含图像。
推荐阅读
- android - 导航架构组件 - 启动画面
- svg - 在 Scala.js 中,修改 svg.Stylable.style
- apache - 是否可以将子目录指向另一个 AWS 实例?
- java - Jersey 端点在 Eclipse 上返回 404
- amazon-web-services - aws cli s3 存储桶下载命令错误
- yii2 - 使用 Yii2 进行评分系统验证
- c# - 针对我的案例进行代码重构
- php - Wordpress - 权限被拒绝
- javascript - Android webview 显示旧网站内容而不是新网站内容
- r - 查找 0 和 1 的所有组合以及特定数量的 1