-
django reservation-list on host page by FBVProject using python/Cloning Airbnb 2021. 3. 17. 07:18
url 설정
config/urls.py
urlpatterns = [ path("reservations/", include("reservations.urls", namespace="reservations")) ]
reservations/urls.py
from django.urls import path from reservations import views app_name = "reservations" urlpatterns = [ path( "<int:pk>/reservation/list/", views.reservationHostList, name="host-list", ), ]
FBV(Function Based View)
reservations/views.py
# reservations/views.py from django.shortcuts import render, redirect, reverse from django.contrib.auth.decorators import login_required from django.contrib import messages from django.core.paginator import Paginator from django.http import Http404 from reservations import models as reservation_models @login_required def reservationHostList(request, pk): reservations = reservation_models.Reservation.objects.filter(room__host_id=pk) qs = [] for reservation in reservations: # Route Protection if reservation.room.host != request.user: raise Http404() if reservation.room not in qs: qs.append(reservation.room) page = request.GET.get("page", 1) if page == "": page = 1 else: page = int(page) page_sector = (page - 1) // 5 page_sector = page_sector * 5 paginator = Paginator(qs, 8, orphans=4) rooms = paginator.get_page(page) return render( request, "pages/reservations/reservation_host_list.html", context={"rooms": rooms, "page_sector": page_sector}, )
templates
pages/reservations/reservation_host_list.html
{% extends 'base.html' %} {% block page_title %} {{request.user}}'s Reservation Host List {% endblock page_title %} {% block content %} <div class="flex flex-col items-center justify-items-center"> <div class="w-10/12"> {% include 'mixins/room_card.html' with rooms=rooms url_page='reservations:host-room-list' %} <div class="flex justify-center mt-20 itmes-center"> {% include 'mixins/page_number.html' with page=rooms %} </div> </div> </div> {% endblock content %}
참고 자료
- 노마드 코더의 Airbnb 클론 강의
소스 코드
github.com/zpskek/airbnb-clone-v3/commit/72b4ab1a1f1caa9872407f005fea17528a2353af
'Project using python > Cloning Airbnb' 카테고리의 다른 글
django reservation-list on room in host by CBV with ListView (0) 2021.03.17 django reservation-list on room in host by FBV (0) 2021.03.17 django reservation-detail by CBV with DetailView (0) 2021.03.17 django reservation-detail by FBV (0) 2021.03.17 django reservation-list by CBV with ListView (0) 2021.03.17