모델의 데이터가 없을 때

Ethan·2024년 1월 17일
0

Dear

목록 보기
2/13

1. 모델의 데이터가 비어있는 경우

모델의 데이터가 비어있는 경우 빈 페이지를 보여주기 보다는, 데이터가 비어있음을 안내해야 한다.

1.1 if문을 통한 구분

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>태그로 등록된 게시글이 없음을 안내한다.

여기서 postsview를 통해 전달받은 데이터를 말한다.


1.2 결과

profile
글로 쓰면 머리 속에 정리가 되...나?

0개의 댓글

관련 채용 정보