ABOUT ME

-

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

    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:user_pk>/reservation/<int:room_pk>/room-list/",
            views.reservationHostRoomList,
            name="host-room-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 reservationListOnRoom(request, user_pk, room_pk):
        qs = reservation_models.Reservation.objects.filter(
            room__host_id=user_pk, room_id=room_pk
        )
        if qs is None:
            messages.error(request, "Rservation does not exist")
            return redirect(reverse("core:home"))
        if qs[0].room.host != request.user:
            raise Http404()
        room_name = qs[0].room.name
    
        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)
        reservations = paginator.get_page(page)
        return render(
            request,
            "pages/reservations/reservation_list_onRoom.html",
            context={"reservations": reservations, "room_name": room_name},
        )
    

    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/91bc1237cc55e994fad83d9659a341433c90b9da

    댓글

Designed by Tistory.