Total values in Django template

Asked

Viewed 320 times

0

I’m developing a forum that on the index page I need to show the subject, how many topics has this subject and how many posts, I can even get back number of topics, but only shows the topic with the id of the subject 1, for example if you have subject 2 will return the amount of subject 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 excerpt 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?

1 answer

2


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

Example of logic:

Using the template tag urls you can link to the respective 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 at documentation.

In his py views. you would be receiving the ID of the topic, subject or post, and filtering by Assunto.objects.filter(id=assunto_id). That one assunto_id will be received by your parameter view.

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 the view py., py. and your html template..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.