我为模板中的项目显示列表创建了模型,视图和模板,但在我运行网站后它没有任何内容。请帮我解决这个问题。
model.py
class waterLevel(models.Model):
height = models.CharField(max_length=250)
date = DateTimeField(max_length=250)
def __str__(self):
# #
return self.height + '-' + self.date
view.py
def compare(request):
all_waterLevels = waterLevel.objects.all()
context = {'all_waterLevels': all_waterLevels}
return render(request, 'Home/compare.html', context)
compare.html
{% if all_waterLevels %}
<ul>
{% for height in all_waterLevels %}
<li><a href="/Home/compare{{ waterLevel.id }}/"> {{ waterLevel.height }}</a></li>
<li><a href="/Home/compare{{ waterLevel.id }}/"> {{ waterLevel.date }}</a></li>
{% endfor %}
</ul>
{% else %}
<h3>no any details</h3>
{% endif %}
答案 0 :(得分:3)
试试这个:
{% if all_waterLevels %}
<ul>
{% for waterLevel in all_waterLevels %}
<li><a href="/Home/compare{{ waterLevel.id }}/"> {{ waterLevel.height }}</a></li>
<li><a href="/Home/compare{{ waterLevel.id }}/"> {{ waterLevel.date }}</a></li>
{% endfor %}
</ul>
{% else %}
<h3>no any details</h3>
{% endif %}