ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • django reservation-list on room in host by CBV with ListView
    Project using python/Cloning Airbnb 2021. 3. 17. 09:26

    url 설정

    config/urls.py

    urlpatterns = [
        path("rooms/", include("rooms.urls", namespace="room"))
    ]

    reservations/urls.py

    from django.urls import path
    from . import views
    
    app_name = "reservations"
    
    urlpatterns = [
    	path(
            "<int:user_pk>/reservation/<int:room_pk>/room-list/",
            views.ReservationListOnRoomView.as_view(),
            name="host-room-list",
        )]

    CBV(Class Based View)

    reservations/views.py

      CBV는 django에서 정의한 여러가지 View들을 이용한다. 그 중 ListView를 이용해서 reservation-list page를 작성한다. ListView는 model을 꼭 정의해줘야 한다. 하지만 model은 DB 안에 있는 모든 record를 가져온다. 내가 원하는 특정 목록만 가져오고 싶을 경우 model을 정의하지 않고 get_queryset() method를 이용해서 특정 list만 반환하면 된다.

    # reservations/views.py
    
    from django.views.generic import ListView
    from django.shortcuts import redirect, reverse
    
    from reservations import models as reservation_models
    from users import mixins
    
    class ReservationListOnRoomView(mixins.LoggedInOnlyView, ListView):
    
        """ Reservation List on Room View Definition """
    
        template_name = "pages/reservations/reservation_list_onRoom.html"
        context_object_name = "reservations"
        paginate_by = 12
        paginate_orphans = 6
        ordering = "created"
    
        def get_queryset(self):
            user_pk = self.kwargs.get("user_pk")
            room_pk = self.kwargs.get("room_pk")
            reservations = reservation_models.Reservation.objects.filter(
                room__host_id=user_pk, room_id=room_pk
            )
    
            if user_pk != self.request.user.pk:
                return redirect(reverse("core:home"))
    
            return reservations
    
        def get_context_data(self, **kwargs):
            user_pk = self.kwargs.get("user_pk")
            room_pk = self.kwargs.get("room_pk")
            reservations = reservation_models.Reservation.objects.filter(
                room__host_id=user_pk, room_id=room_pk
            )
            room_name = reservations[0].room.name
            context = super().get_context_data()
            context["room_name"] = room_name
            return context
    

    templates

    pages/reservations/reservation_list_onRoom.html

    {% extends 'base.html' %}
    
    {% block page_title %}
        {{request.user}}'s Reservation Room List
    {% endblock page_title %}
    
    {% block content %}
    
    <div class="flex flex-col items-center justify-items-center">
        <div class="w-10/12">
            <div class="mb-8 mx-4">
                <h1 class="font-bold text-4xl ">{{room_name}}</h1>
            </div>
            <table class="w-full" border="1" bordercolor="blue" width="300px" height="150px" align = "center">
                <th>Room Name</th>
                <th>Guest</th>
                <th width="180px">Check In</th>
                <th width="180px">Check Out</th>
                <th width="180px">Status</th>
                <th width="180px">Confirm</th>
                <th width="180px">Cancel</th>
                {% for reservation in reservations %}
                    <tr align="center">
                        <td class="text-base font-medium">{{reservation.room.name}}</td>
                        <td class="text-base font-medium">{{reservation.guest}}</td>
                        <td class="text-base font-medium">{{reservation.check_in}}</td>
                        <td class="text-base font-medium">{{reservation.check_out}}</td>
                        <td class="text-base font-medium">{{reservation.status|capfirst}}</td>
                        <td class="">
                            <button class="button w-3/4 mt-0 text-xs ">
                                <a href="#">Confirm</a>
                            </button>
                        </td>
                        <td>
                            <button class="button w-3/4 text-xs mt-0  bg-red-500">
                                <a  href="#">Cancel</a>
                            </button>
                        </td>
                    </tr>
                {% endfor %}
            </table>
            <div class="flex justify-center mt-6 itmes-center">
                <button class="button bg-gray-700 w-1/6">
                    <a href="{% url 'reservations:host-list' user.pk %}">Back</a>
                </button>
            </div>
            <div class="flex justify-center mt-20 itmes-center">
                {% include 'mixins/page_number.html' with page=reservations %}
            </div>
        </div>
    </div>
    {% endblock content %}

     

    참고 자료

    • 노마드 코더의 Airbnb 클론 강의

    소스 코드

    github.com/zpskek/airbnb-clone-v3/commit/bf2d1ec013edfc664d3f96dec5f07c3ba2615953

    댓글

Designed by Tistory.