我正面临在django python中实现动态推荐和旋转木马,意味着我需要在下面提到的模型中提供内容我提到我的实施代码
template.html
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner">
{% for p in tests %}
{% if forloop.counter == 1 %}
<div class="item active">
{% else %}
<div class="item">
{% endif %}
<h3>{{ p.title }}</h3>
</div>`enter code here`
</div>
{% endfor %}
</div>
</div>
view.py
def home(request):
test=Testimonials.objects.all().order_by('id')[:13]
template = loader.get_template('hme/home.html')
context = {
'tests':test,
}
return HttpResponse(template.render(context, request))
答案 0 :(得分:0)
我不知道你真正想做什么,但你的 views.py 可以重写如下:
from django.shortcuts import render
def home(request):
tests = Testimonials.objects.order_by('id')[:13]
return render(request, 'hme/home.html', {'tests': tests})