I have a little problem with annotate. I want to display records from my class Kategorie in the main html file. I used the annotate method to take the query from db. I used in the second class Firmy the ForeignKey to class Kategorie. Now I dont know how to display for example how many websites added in the class Firmy are in the for example in category Business. Now I have something like: "Business (2)(3)(4)" when I used annotate with count by id. This is my models.py
from django.db import models
from django.utils import timezone
class Kategorie(models.Model):
glowna = models.CharField(max_length=150, verbose_name='Kategoria')
class Meta:
verbose_name='Kategoria'
verbose_name_plural='Kategorie'
def __str__(self):
return self.glowna
class Witryna(models.Model):
nazwa = models.CharField(default="", max_length=150, verbose_name = 'Nazwa strony')
adres_www = models.CharField(max_length=70, verbose_name='Adres www')
slug = models.SlugField(max_length=250, verbose_name='Przyjazny adres url')
email = models.CharField(max_length=100, verbose_name='Adres e-mail')
text = models.TextField(max_length=3000, verbose_name='Opis strony')
kategoria = models.ForeignKey(Kategorie, verbose_name='Kategoria')
data_publikacji = models.DateTimeField(blank=True, null=True, verbose_name='Data publikacji')
class Meta:
verbose_name='Strona www'
verbose_name_plural = 'Strony www'
def publikacja(self):
self.data_publikacji=timezone.now()
self.save()
def __str__(self):
return self.nazwa
And some part from views.py
from django.db.models import Count
wpisy_kat = Kategorie.objects.annotate(cnt_witryna=Count('Witryna'))
So what kind of method or tags I have to use to display for example:
Business(34)
Industry(21)
Health Care(11)
where the name od category is field from class Kategorie and integer is a result from query to database how many websites are in for example Business category?
My html file is:
{% for kategoria in kategorie %}
<table>
<tr>
<td>
<li><a href="{% url 'detale_kat' slug_kat=kategoria.slug_kat %}">{{ kategoria.glowna|linebreaksbr }} </a></li>
{% for wpis in wpisy_kat %}
{{ wpis }} ({{ cat.cnt_witryna }})
{% endfor %}
</td>
</tr>
</table>
{% endfor %}
and the main html file:
{% include 'firmy/header.html' %}
<html>
<body>
<p>
<center>
<ul id="menu">
<li><a href="link1.html">Strona główna</a></li>
<li><a href="link2.html">Jak dodać stronę</a></li>
<li><a href="link3.html">Regulamin</a></li>
<li><a href="link4.html">Kontakt</a></li>
</ul>
</center>
<div class="glowna">
<div class="lewe_menu">
<h3><center>Ostatnio dodane</center></h3>
{%include 'firmy/widok_strony.html'%}
</div>
<div class="srodek">
<h3><center>Kategorie</center></h3>
<center>{%include 'firmy/widok_kategorii.html'%} </center>
</div>
<div class="prawe_menu">
<h3><center>Reklama</center></h3>
<center>Tutaj wpisz kod reklamy </center>
</div>
{% include 'firmy/footer.html' %}
</div>
</body>
</html>
view.py file
from django.shortcuts import render, get_object_or_404
from .models import Witryna, Kategorie
from django.utils import timezone
from django.db.models import Count
def widok_strony(request):
firmy = Witryna.objects.filter(data_publikacji__lte=timezone.now()).order_by('data_publikacji')
return render(request, 'firmy/widok_strony.html', {'firmy': firmy})
def widok_kategorii(request):
kategorie = Kategorie.objects.all()
wpisy_kat = Witryna.objects.annotate(cnt_kategoria=Count('kategoria'))
return render(request, 'firmy/widok_kategorii.html', {'kategorie': kategorie, 'wpisy_kat': wpisy_kat,})
def index(request):
firmy = Witryna.objects.filter(data_publikacji__lte=timezone.now()).order_by('data_publikacji')
kategorie = Kategorie.objects.order_by('glowna')
wpisy_kat = Witryna.objects.annotate(cnt_witryna=Count('kategoria'))
return render(request, 'firmy/index.html', {'kategorie': kategorie, 'wpisy_kat': wpisy_kat, 'firmy': firmy})
def detale_strony(request, slug):
det_wpisu = get_object_or_404(Witryna, slug=slug)
firmy = Witryna.objects.filter(data_publikacji__lte=timezone.now()).order_by('data_publikacji')
return render(request, 'firmy/detale_strony.html', {'det_wpisu': det_wpisu, 'firmy': firmy})
def detale_kat(request, slug_kat):
det_kategorii = get_object_or_404(Kategorie, slug_kat=slug_kat)
firmy = Witryna.objects.filter(data_publikacji__lte=timezone.now()).order_by('data_publikacji')
return render(request, 'firmy/detale_kat.html', {'det_kategorii': det_kategorii, 'firmy': firmy})
{{ wpis }} ({{ cat.cnt_witryna }}): replacecatbywpis, then you are mixing two for loops, the annotate method already gives you the list of categories, you should try with only mine to check, and then add the<a>tag withwpis.slug_kat(no need to use a table)