Issue
How to fix that error?
In template C:\Users\webst\webstudioamerica\webstudio\templates\index.html, error at line 441 Python: 'Videos' object is not iterable This code is not working:
439 {% for chunk in videos|slice:":3" %}
440 <div class="row">
441 {% for video in chunk %}
442 <div class="col-xs-12 col-lg-4">
443 <div class="video-container">
444 <iframe class="video" src="{{ video.video_id }}" allowfullscreen></iframe>
445 </div>
446 </div>
447 {% endfor %}
448 </div>
449 {% endfor %}
This one is working:
{% for video in videos %}
<li>{{ video.video_id }}</li>
{% endfor %}
views.py is the same:
from django.shortcuts import render
from .models import Videos
def index(request):
videos = Videos.objects.all()
return render(request, 'index.html', {'videos': videos})
Solution
Your code here actually confirms that each entry in the QuerySet
is not iterable:
{% for video in videos %}
<li>{{ video.video_id }}</li>
{% endfor %}
So when you slice the QuerySet
here, each chunk
is now just one entry from the sliced QuerySet
just like video
is as shown in the above.
Hence chunk
in the below code represents just one entry gotten from each iteration over the QuerySet
Videos.objects.all()
.
{% for chunk in videos|slice:":3" %}
chunk
is not iterable. That is why this line throws the errors you got:
{% for video in chunk %}
Answered By - Chukwujiobi Canon
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.