Maxime Michel
Maxime Michel

Reputation: 615

Check database element isn't empty in HTML with Django

I have some display in a website I'm working on that works correctly but shows me everything even if some elements are null. I'm using Django and have the below model with which I want to change the display:

class Exchange(models.Model):
    YEAR_CHOICE = ((4, '4TC'),
        (5, '5TC'))

    SEMESTER_CHOICE = ((1, 'S1'),
        (2, 'S2'))

    GRADE = ((1, '1'),
        (2, '2'),
        (3, '3'),
        (4, '4'),
        (5, '5'))
    ID = models.AutoField(primary_key=True)
    Year = models.IntegerField(default=-1, choices=YEAR_CHOICE)
    StartDate = models.DateField()
    EndDate = models.DateField()
    Semester = models.IntegerField(default=-1, choices=SEMESTER_CHOICE)
    Visa = models.BooleanField(default=False)
    Comment = models.CharField(max_length=1000, null=True, blank=True)
    VisaMonths = models.IntegerField(null=True, blank=True, default=-1,)
    VisaWeeks = models.IntegerField(null=True, blank=True, default=-1,)
    VisaDays = models.IntegerField(null=True, blank=True, default=-1)
    Rent = models.IntegerField(null=True, blank=True, default=-1)
    MonthlyExpenses = models.IntegerField(null=True, blank=True, default=-1)
    NightLifeGrade = models.IntegerField(null=True, blank=True, default=-1,choices=GRADE)
    CulturalLifeGrade = models.IntegerField(null=True, blank=True, default=-1,choices=GRADE)
    Security = models.IntegerField(null=True, blank=True, default=-1,choices=GRADE)
    Student = models.ForeignKey('Student', on_delete=models.CASCADE)
    University = models.ForeignKey('University', on_delete=models.CASCADE)

    def __str__(self):
        return str(self.ID)

In my HTML page, I have the following:

<button class="accordion">Commentaires</button>
    <div class="panel">
        {% for e in ex %}
        <h5>{{e.Student.Name}} {{e.Student.Surname}}, parti en {{e.StartDate}} ({{e.Year}}A {{e.Student.INSADepartement}} S{{e.Semester}})</h5>
            <blockquote>{{e.Comment}}</blockquote>
        {% endfor %}
    </div>

I would like to only display the information in the button if e.Comment isn't null. I tried adding {% if(e.Comment) %} ... {% endif %} in the for loop but I keep getting a Could not parse the remainder error.

Any ideas on how I could perform this check ?

Upvotes: 0

Views: 52

Answers (1)

Mukul Kumar
Mukul Kumar

Reputation: 2103

Change your html page, you just need to change your if condition.

<button class="accordion">Commentaires</button>
    <div class="panel">
        {% for e in ex %}
        <h5>{{e.Student.Name}} {{e.Student.Surname}}, parti en {{e.StartDate}} ({{e.Year}}A {{e.Student.INSADepartement}} S{{e.Semester}})</h5>
            {% if e.Comment %}
            <blockquote>{{e.Comment}}</blockquote>
            {% endif %}
        {% endfor %}
    </div>


And if you don't want to display anything, if there is no comment. Then your html page should be like this


<button class="accordion">Commentaires</button>
{% for e in ex %}
    {% if e.Comment %}
    <div class="panel">
        <h5>{{e.Student.Name}} {{e.Student.Surname}}, parti en {{e.StartDate}} ({{e.Year}}A {{e.Student.INSADepartement}} S{{e.Semester}})</h5>
        <blockquote>{{e.Comment}}</blockquote>
    </div>
    {% endif %}
{% endfor %}

Upvotes: 1

Related Questions