[Python Django] 2-5. FBV List, Pagination 구현하기
2022. 3. 26. 09:32ㆍPython/Django Framework
반응형
veiws.py 수정
def list(request):
# user 정보
user_data = User.objects.all().order_by('-id')
# paginator
paginator = Paginator(user_data, 5) # user_data를 기반으로 paginator생성, 페이지당 5개
page = int(request.GET.get('p', 1)) # 현재 페이지 세팅, 없으면 1
user_list = paginator.get_page(page) # 현재 페이지의 user_list 생성
return render(request, 'list.html', {'user_list': user_list})
usrls.py 수정
from django.urls import path
from user import views
urlpatterns = [
path('detail/', views.detail),
path('create/', views.create),
path('update/', views.update),
path('login/', views.login),
path('logout/', views.logout),
path('list/', views.list) # 추가
]
list.html 추가
{% extends "base.html" %}
{% block contents %}
<div class="row mt-5">
<div class="col-12">
<h1>목록</h1>
</div>
</div>
<div class="row mt-5">
<div class="col-12">
<table class="table table-hover">
<thead>
<tr>
<th scope="col">이용자 명</th>
<th scope="col">이메일</th>
<th scope="col">등록일</th>
<th scope="col">최종수정일</th>
</tr>
</thead>
<tbody>
{% for user in user_list %}
<tr scope="row">
<td>{{ user.id }}</td>
<td><a href="/user/detail">{{ user.user_email }}</a></td>
<td>{{ user.insert_date|date:"Y-m-d" }}</td>
<td>{{ user.update_date|date:"Y-m-d" }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="row col-12">
<div class="col-4">
{% if request.session.user_id %}
<a class="btn btn-outline-primary" href="/user/logout">로그아웃</a>
{% else %}
<a class="btn btn-outline-primary" href="/user/login">로그인</a>
{% endif %}
</div>
<div class="col-4">
<nav>
<ul class="pagination justify-content-center">
{% if page_obj.has_previous %}
<li class="page-item">
<a class="page-link" href="?p={{ page_obj.previous_page_number }}">«</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" href="javascript:return false">«</a>
</li>
{% endif %}
<li class="page-item active">
<a class="page-link" href="#">{{ user_list.number }}
/ {{ user_list.paginator.num_pages }}</a>
</li>
{% if page_obj.has_next %}
<li class="page-item">
<a class="page-link" href="?p={{ page_obj.next_page_number }}">»</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" href="javascript:return false">»</a>
</li>
{% endif %}
</ul>
</nav>
</div>
<div class="col-4">
<a href="/user/create/" class="btn btn-primary float-end">등록</a>
</div>
</div>
</div>
{% endblock %}
반응형
'Python > Django Framework' 카테고리의 다른 글
[Python Django] 3-1. CBV FormView를 이용한 Create 구현 (0) | 2022.03.26 |
---|---|
[Python Django] 2-6. FBV Delete 구현하기 (0) | 2022.03.26 |
[Python Django] 2-4. FBV Form을 이용한 Update 구현 (0) | 2022.03.26 |
[Python Django] 2-3. FBV Form을이용한 Login, Logout 구현 (0) | 2022.03.26 |
[Python Django] 2-2. FBV 함수를 이용한 Create 구현 (0) | 2022.03.26 |