alliocha1805
alliocha1805

Reputation: 9

Trouble with an if statement in Django template : two equal string are not equal

I'm trying to build a table on my Django Website that will show all the client of a consultant company and for each company how many contract they still have to date

My code seems to work but it show me two identical string and work as if they're not equal

I tried to do this logic : I DO a for loop on every client to build my table (a row for each client)

On my template for every client i'll do a forloop of every contrat to check if the contract-client is the client : if so i increment a var and at the end of this sub forloop i can show how much mission exist for each client

Here's the code piece of my template :

    <table class="contenu">
        <thead>
            <th>Nom client</th>
            <th>Domaine</th>
            <th> Missions en cours </th>
            <th>Modification </th>
        </thead>
        <tbody>
    {% for client in listeClients %}
            <tr>
            <td>{{ client.nomClient }}</td>
            <td>{{ client.domaineClient }}</td>
            {% with compteur=0 %}
            {% for mission in missionsEnCours %}
                {% if client.nomClient == mission.client %}
                    {{compteur|augment}}
                    <script>alert("Ca incrémente")</script>
                {% else %}
                    <script>alert("{{ client.nomClient }} ---et--- {{ mission.client }}")</script>
                {% endif %}
            {% endfor %}
            <td>{{ compteur }}</td>
            <td><a href="">MODIFIER</a></td>
            </tr>
            {% endwith %}
    {% endfor %}
        </tbody>
    </table>

I've tried to debug with the alert and sometimes it show me two identical string but the var compteur never increment and it never show the alert "Ca incremente"

example of identical strings showing in the alert:

https://zupimages.net/up/19/41/p4aj.png

What am I missing ?

My models :

class client(models.Model):
    nomClient = models.CharField(max_length=200)
    domaineClient = models.CharField(max_length=200)
    def __str__(self):
        return self.nomClient

class experiences(models.Model):
    nomMission = models.CharField(max_length=300)
    client = models.ForeignKey(client, on_delete=models.CASCADE)
    serviceConcerne = models.ForeignKey(services, on_delete=models.CASCADE)
    dateDebut = models.DateField('date de début de mission')
    dateFin = models.DateField('date de fin de mission', blank=True, null=True)
    nbJourHomme = models.IntegerField()
    contexteMission = models.TextField()
    descriptifMission = models.TextField()
    environnementMission =  models.TextField()
    collaborateurMission = models.ForeignKey(collaborateurs, on_delete=models.CASCADE, default='')
    def __str__(self):
        return self.nomMission

My view :


def client_all(request):
    listeClients = client.objects.order_by("nomClient")
    missionsEnCours = experiences.objects.filter(dateFin__gte=datetime.date.today()).order_by("dateFin")
    return render(request, 'collab/client.html', {'listeClients': listeClients, 'missionsEnCours':missionsEnCours})

Thanks a lot :)

Upvotes: 0

Views: 362

Answers (1)

michjnich
michjnich

Reputation: 3385

mission.client is an instance of client.

client.nomClient is a text string.

Try mission.client.nomClient == client.nomClient

Upvotes: 1

Related Questions