Total values in django template

0

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?

    
asked by anonymous 26.11.2017 / 03:26

1 answer

2

To load the subjects, topics or posts that the user wants to read, you must pass a parameter to view to filter the selected items.

Logic example:

Using the template urls template you can link to its content clicked:

{% for assunto in assuntos %}
    <a href="{% url 'topico' assunto.topico.id %}">
{% endfor %}

In this case it would be taking the URL referring to the topic as defined in urls.py and generating the link. Information on documentation .

In your views.py you would be getting the topic ID, subject or post, and filtering by Assunto.objects.filter(id=assunto_id) . This assunto_id is being received by the view parameter.

def index(request, assunto_id):
    ...

The problem you are facing can only be solved by reading the documentation that shows step by step how to develop the code responsible for loading the data through parameters passed by the URL. This whole process will involve view.py , urls.py and your template.html .

    
27.11.2017 / 01:12