Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查数据库元素isn';使用Django在HTML中不能为空_Javascript_Html_Django - Fatal编程技术网

Javascript 检查数据库元素isn';使用Django在HTML中不能为空

Javascript 检查数据库元素isn';使用Django在HTML中不能为空,javascript,html,django,Javascript,Html,Django,我在一个网站上有一些显示,可以正常工作,但即使某些元素为null,也会显示所有内容。我使用的是Django,我想用以下型号更改显示: class Exchange(models.Model): YEAR_CHOICE = ((4, '4TC'), (5, '5TC')) SEMESTER_CHOICE = ((1, 'S1'), (2, 'S2')) GRADE = ((1, '1'), (2, '2'),

我在一个网站上有一些显示,可以正常工作,但即使某些元素为
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>
评论
{ex%中的e为%1}
{{e.Student.Name}{{e.Student.姓氏},parti en{e.StartDate}({e.Year}A{{e.Student.insadepartment}{{e.estime})
{{e.Comment}}
{%endfor%}
如果
e.Comment
不是
null
,我只想在按钮中显示信息。我尝试添加
{%if(e.Comment)%}。。。{%endif%}
在for循环中,但我一直得到一个
无法解析剩余部分的
错误

关于如何执行此检查,您有什么想法吗?

更改您的html页面,您只需要更改您的if条件。
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 %}
评论 {ex%中的e为%1} {{e.Student.Name}{{e.Student.姓氏},parti en{e.StartDate}({e.Year}A{{e.Student.insadepartment}{{e.estime}) {%if e.Comment%} {{e.Comment}} {%endif%} {%endfor%} 如果你不想显示任何东西,如果没有评论。那么你的html页面应该是这样的 评论 {ex%中的e为%1} {%if e.Comment%} {{e.Student.Name}{{e.Student.姓氏},parti en{e.StartDate}({e.Year}A{{e.Student.insadepartment}{{e.estime}) {{e.Comment}} {%endif%} {%endfor%}
这在某种程度上是可行的,但如果没有评论,我不想显示任何内容。这仍然会显示部分信息。也就是说,我可以简单地将其移到上面。谢谢:D