I'm developing a forum that in the index page I need to show the subject, how many topics have this subject and how many posts, I even get back number of topics, but only shows the topic with the subject id 1, for example if you have subject 2 will return the subject quantity 1.
this is my views
def index(request):
topico = Topico.objects.all()
total = len(topico)
assunto_jogo = Assunto.objects.filter(id_categoria=1)
context = {
'assunto': assunto_jogo,
'topicos': topico,
'total': total
}
return render(request, "index.html", context)
and this is the portion of the template where the quantity will be shown
{% for assuntos in assunto %}
<tbody>
<tr>
<td class="text-center" style="width: 40px;"><i class="fa fa-globe fa-2x text-muted"></i></td>
<td>
<h5>
<a href="topic.html">{{ assuntos }}</a>
<small class="d-block">
{{ assuntos.descricao }}
</small>
</h5>
</td>
<td class="text-center hidden-xs hidden-sm">
{% if topicos.id_assunto == 1: %}
{{ total }}
{% endif %}
</td>
<td class="text-center hidden-xs hidden-sm">
1342
</td>
<td class="hidden-xs hidden-sm">por
<b>Game Master</b>
<br>
<small><i>1 nov 2017, 14:30</i></small>
</td>
</tr>
</tbody>
{% endfor %}
and the models
class Topico(models.Model):
titulo = models.CharField('Título', max_length=100)
mensagem = models.TextField('Mensagem')
data_criacao = models.DateTimeField('data de criação', auto_now_add=True, blank=True, null=True)
avatar = models.ImageField(upload_to='core/images', verbose_name='Imagem', blank=True, null=True)
id_usuario = models.ForeignKey(Adm)
id_assunto = models.ForeignKey(Assunto)
def __str__(self):
return self.titulo
class Post(models.Model):
mensagem = models.TextField('Mensagem')
avatar = models.ImageField(upload_to='core/images', verbose_name='Imagem', blank=True, null=True)
data_criacao = models.DateTimeField('data de criação', auto_now_add=True)
id_usuario = models.ForeignKey(Adm)
id_topico = models.ForeignKey(Topico)
id_assunto = models.ForeignKey(Assunto)
def __str__(self):
return self.mensagem
How can this be done?