지금까지 CRUD 에 해당하는 view 를 작성하였다
계정을 만들떄에는 단일 객체(single object)만 해당되었다
최종적으로 게시판 같은 형태의 Article 을 만드는 것이 목표이기 떄문에 여러 객체(multiple object)를 다룰 수 있는 view 가 필요
여기서 나오는게 List view 이다. -> 장고에서 제공
추가적으로 pagination (페이지화 시킨다 -> 한페이지에 몇개의 게시글을 보여줄 것인지)

ListView 코드 위해 주요하게 사용할 템플릿의 두가지 html

article_List

page_obj

# articleapp/views.py
from msilib.schema import ListView
...
# 추가
class ArticleListView(ListView):
model = Article
context_object_name = 'article_List'
template_name = 'articleapp/list.html'
paginate_by = 5
from django.urls import path
from django.views.generic import TemplateView
from articleapp.views import ArticleCreateView, ArticleDetailView, ArticleUpdateView, ArticleDeleteView, ArticleListView
app_name = 'articleapp'
urlpatterns = [
path('list/', ArticleListView.as_view(), name='list'), # 수정
path('create/', ArticleCreateView.as_view(), name='create'),
path('detail/<int:pk>', ArticleDetailView.as_view(), name='detail'),
path('update/<int:pk>', ArticleUpdateView.as_view(), name='update'),
path('delete/<int:pk>', ArticleDeleteView.as_view(), name='delete'),
]
<!--articleapp/templates/articleapp/list.html-->
...
<!--html 부분 수정-->
<div class="container">
{% for article in article_list %}
<a href=" {% url 'articleapp:detail pk=article.pk %}">
<div>
<img src="{{ article.image.url }}" alt="">
</div>
</a>
</div>
...
<!--templates/snippets/card.html-->
<div>
<img src="{{ article.image.url }}" alt="">
</div>
<!--articleapp/templates/articleapp/list.html-->
...
<div class="container">
{% for article in article_list %}
<a href=" {% url 'articleapp:detail pk=article.pk %}">
{% include 'snippets/card.html' with article=article %}
</a>
</div>
...
...
{% if article_list %}
<div class="container">
{% for article in article_list %}
<a href=" {% url 'articleapp:detail' pk=article.pk %}">
{% include 'snippets/card.html' with article=article %}
</a>
{% endfor %}
</div>
<script src="{% static 'js/magicgrid.js' %}"></script> <!--js 를 컨테이너 내부로 이동시키기-->
{% else %}
<div class="text-center">
<h1>No Articles YET! </h1>
</div>
{% endif %}
...


http://127.0.0.1:8000/articles/list/page=2
{% if article_list %}
<div class="container">
{% for article in article_list %}
<a href=" {% url 'articleapp:detail' pk=article.pk %}">
{% include 'snippets/card.html' with article=article %}
</a>
{% endfor %}
</div>
<script src="{% static 'js/magicgrid.js' %}"></script> <!--js 를 컨테이너 내부로 이동시키기-->
{% else %}
<div class="text-center">
<h1>No Articles YET! </h1>
</div>
{% endif %}
<!-- 추가 -->
{% include 'snippets/pagination.html' with page_obj=page_obj %}
<!--templates/snippets/pagination.html-->
<div style="text-align: center; margin:1rem 0;">
{% if page_obj.has_previous %} <!--현재 페이지 page_obj 객체가 이전 페이지를 가지고 있다면-->
<!--get 방식으로 이전 페이지의 번호를 넘겨주면서 이전 페이지로 넘어가는 링크를 만들어줌-->
<a href="{% url 'articleapp:list' %}?page={{ page_obj.previous_page_number }}"
class="btn btn-secondary rounded-pill">
{{ page_obj.previous_page_number }}
</a>
{% endif %}
<!-- 현재 있는 페이지 -->
<a href="{% url 'articleapp:list' %}?page={{ page_obj.number }}"
class="btn btn-secondary rounded-pill active">
{{ page_obj.number }}
</a>
{% if page_obj.has_next %} <!-- 다음 페이지가 있다면 -->
<!-- 다음페이지로 향하는 링크 만들어주기-->
<a href="{% url 'articleapp:list' %}?page={{ page_obj.next_page_number }}"
class="btn btn-secondary rounded-pill active">
{{ page_obj.next_page_number }}
</a>
{% endif %}
</div>
{% extends 'base.html'%}
{% block content %}
<div>
<div style="text-align: center; max-width: 700px; margin: 4rem auto;">
<!--게시글의 제목-->
<h1>
{{ target_article.title }}
</h1>
<!-- 닉네임 표시 -->
<h5>
{{ target_article.writer.profile.nickname }}
</h5>
<hr>
<!--게시글의 이미지-->
<img style="width: 100%; border-radius: 1rem; margin: 2rem 0;"
src=" {{ target_article.image.url }}" alt="">
<!-- 게시글의 내용 -->
<p>
{{ target_article.content }}
</p>
<!-- 인증 과정 포함-->
{% if target_article.writer == user %}
<!-- 게시글 업데이트-->
<a href="{% url 'articleapp:update' pk=target_article.pk %}"
class="btn btn-primary rounded-pii col-3">
Update
</a>
<!-- 게시글 삭제 -->
<a href="{% url 'articleapp:delete' pk=target_article.pk %}"
class="btn btn-danger rounded-pii col-3">
Delete
</a>
{% endif %}
<hr> <!-- 밑에 코멘트가 들어갈 공간 분리-->
</div>
</div>
{% endblock %}