Reputation: 21
Hi I am newbie and i am getting error "'Zagrania' object is not iterable". I want to get most viewed object and display it differently (wyswietlenia-views of object)
views.py
def zagrania(request):
zagrania = Zagrania.objects.all().order_by('-data')
najepszezagranie = Zagrania.objects.all().order_by('-wyswietlenia').first()
return render(request, 'zagrania/zagrania.html', { 'zagrania':zagrania, 'najepszezagranie':najepszezagranie})
models.py
class Zagrania(models.Model):
tytul = models.CharField(max_length=70)
data = models.DateTimeField(auto_now_add=True)
autor = models.ForeignKey(User,on_delete=models.CASCADE,default=None)
opis = models.TextField(max_length=276, default='')
wyswietlenia = models.PositiveIntegerField(default=0)
filmik = models.FileField(upload_to="static/filmiki")
#votes= models.IntegerField(default=0)
template
{% for zagranie in najepszezagranie %}
<a href="{% url 'zagrania_detail' zagranie.id%}">
<video>
<source src="{{ zagranie.filmik.url }}" type="video/mp4"></source>
</video>
</a>
<a href="{% url 'zagrania_detail' zagranie.id%}">{{ zagranie.tytul }}</a>
{% endfor %}
Upvotes: 2
Views: 65
Reputation: 476557
Well the najepszezagranie
object is not a QuerySet
(or any other collection) of Zagrania
objects, it is a Zagrania
object. So the {% for zagranie in najepszezagranie %}
makes no sense.
You thus can use najepszezagranie
as an object (well it is an object), and remove the {% for ... %}
loop:
<!-- template.html -->
<a href="{% url 'zagrania_detail' najepszezagranie.id %}"><video>
<source src="{{ najepszezagranie.filmik.url }}" type="video/mp4"></source>
</video></a>
<a href="{% url 'zagrania_detail' najepszezagranie.id %}">{{ najepszezagranie.tytul }}</a>
Upvotes: 1