ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • django reservation-list by FBV
    Project using python/Cloning Airbnb 2021. 3. 15. 11:24

    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(
            "user/<int:user_pk>/",
            views.reservationList,
            name="list",
        ),
    ]
    

    FBV(Function Based View)

    reservations/views.py

    # reservations/views.py
    
    from django.shortcuts import render
    from django.contrib.auth.decorators import login_required
    from django.contrib import messages
    from django.http import Http404
    from django.core.paginator import Paginator
    
    from reservations import models as reservation_models
    
    
    @login_required
    def reservationList(request, user_pk):
        qs = reservation_models.Reservation.objects.filter(guest_id=user_pk)
    
        if not qs:
            # if 'qs' Queryset is empty, execute this code.
            return render(
                request,
                "pages/reservations/reservation_list.html",
                context={"reservations": qs},
            )
    
        for reservation in qs:
            # Route Protection
            if reservation.guest != request.user:
                raise Http404()
    
        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.html",
            context={"reservations": reservations, "page_sector": page_sector},
        )

    templates

    pages/reservations/reservation_list.html

    {% extends 'base.html' %}
    
    {% load remainder_op %}
    
    {% block page_title %}
        {{request.user}}'s Reservation List
    {% endblock page_title %}
    
    {% block content %}
        <div class="flex flex-col items-center justify-items-center">
            <div class="w-10/12">
                {% include 'partials/reservation_card.html' %}
                <div class="flex justify-center mt-20 itmes-center">
                    {% include 'mixins/page_number.html' with page=reservations %}
                </div>
            </div>
        </div>
    {% endblock content %}

    partials/reservation_card.html

    <ul class="grid grid-cols-4 grid-rows-2-300px auto-rows-300px gap-4 gap-y-6">
        {% for reservation in reservations %}
            <li class="w-full">
                <a href="#">
                    <div class="rounded-lg bg-cover bg-center h-3/4 mb-2" style="background-image:url('{{reservation.room.get_first_photo}}')">
                        <div class="flex items-center {% if reservation.room.host.superhost %}justify-between{% else %}justify-end{% endif %}">
                            {% if reservation.room.host.superhost %}
                                <span class="bg-white p-2 ml-2  mt-2 rounded-lg font-semibold justify-start">Superhost</span>
                            {% endif %}
                            <i class="far fa-heart mr-2 mt-2 text-2xl text-white justify-end text-center"></i>
                        </div>
                    </div>
                    <div class="h-1/4">
                        <div class="mb-1">
                            <i class="fas fa-star text-red-500"></i>
                            <span class="review-points">{{reservation.room.get_review_points}}</span>
                            <span class="text-gray-400">({{reservation.room.reviews.count}})</span>
                        </div>
                        <div class="truncate">
                            {{reservation.room.country}} &bull; {{reservation.room.address}}
                        </div>
                    </div>
                </a>
            </li>
        {% endfor %}
    </ul>

    참고 자료

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

    소스 코드

    github.com/zpskek/airbnb-clone-v3/commit/36e8a2cfa45fc0311871e3c9b0788329c618a5e9

    'Project using python > Cloning Airbnb' 카테고리의 다른 글

    django reservation-detail by FBV  (0) 2021.03.17
    django reservation-list by CBV with ListView  (0) 2021.03.17
    django 'reserve a room' by FBV  (0) 2021.03.13
    django delete-photo by FBV  (0) 2021.03.12
    django edit-photo by CBV  (0) 2021.03.12

    댓글

Designed by Tistory.