모델의 데이터가 비어있는 경우 빈 페이지를 보여주기 보다는, 데이터가 비어있음을 안내해야 한다.
if문을 통해 작성된 html을 살펴본다.
<!--post_list.html-->
{% block content %}
<div class="btn_post">
<a href={% url "post-create" %}>기록하기</a>
</div>
{% if posts %}
<div class="post_container">
{% for post in posts %}
<div class="post"><a href="{% url "post-detail" post.id %}">
<h2 class="title">{{post.title}}</h2>
<p class="date">{{post.dt_created}}</p>
<p class="text">{{post.content}}</p>
</a></div>
{% endfor %}
</div>
{% else %}
<div class="blank">
<p>
등록된 게시글이 없습니다.<br>
게시글을 등록해보세요.
</p>
</div>
{% endif %}
{% endblock content %}
템플릿 태그 if
를 활용
posts가 있다면, 각 포스트의 내용을 불러오고
posts가 없다면
<p>
태그로 등록된 게시글이 없음을 안내한다.
여기서
posts
는view
를 통해전달받은 데이터
를 말한다.