猿问

使用 Django 检查 HTML 中的数据库元素不为空

我在我正在处理的网站上有一些显示,它可以正常工作,但即使某些元素是null. 我正在使用 Django 并具有以下模型,我想用它来更改显示:


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)

在我的 HTML 页面中,我有以下内容:


<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>

如果e.Comment不是,我只想在按钮中显示信息null。我尝试{% if(e.Comment) %} ... {% endif %}在 for 循环中添加,但我一直收到 Could not parse the remainder错误消息。


关于如何执行此检查的任何想法?


一只名叫tom的猫
浏览 246回答 1
1回答

素胚勾勒不出你

Change your html page, you just need to change your if condition.<button class="accordion">Commentaires</button>&nbsp; &nbsp; <div class="panel">&nbsp; &nbsp; &nbsp; &nbsp; {% for e in ex %}&nbsp; &nbsp; &nbsp; &nbsp; <h5>{{e.Student.Name}} {{e.Student.Surname}}, parti en {{e.StartDate}} ({{e.Year}}A {{e.Student.INSADepartement}} S{{e.Semester}})</h5>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% if e.Comment %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <blockquote>{{e.Comment}}</blockquote>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% endif %}&nbsp; &nbsp; &nbsp; &nbsp; {% endfor %}&nbsp; &nbsp; </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 %}&nbsp; &nbsp; {% if e.Comment %}&nbsp; &nbsp; <div class="panel">&nbsp; &nbsp; &nbsp; &nbsp; <h5>{{e.Student.Name}} {{e.Student.Surname}}, parti en {{e.StartDate}} ({{e.Year}}A {{e.Student.INSADepartement}} S{{e.Semester}})</h5>&nbsp; &nbsp; &nbsp; &nbsp; <blockquote>{{e.Comment}}</blockquote>&nbsp; &nbsp; </div>&nbsp; &nbsp; {% endif %}{% endfor %}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答