-
django fav-list by CBV with ListViewProject using python/Cloning Airbnb 2021. 4. 23. 16:02
url 설정
config/urls.py
urlpatterns = [ path("lists/", include("lists.urls", namespace="lists")) ]
lists/urls.py
from django.urls import path from lists import views app_name = "lists" urlpatterns = [ path("favs/", views.FavsList.as_view(), name="favs"), ]
CBV(Class Based View)
lists/views.py
CBV는 django에서 정의한 여러가지 View들을 이용한다. 그 중 ListView를 이용해서 fav-list page를 작성한다. ListView는 model을 꼭 정의해줘야 한다. 하지만 model은 DB 안에 있는 모든 record를 가져온다. 내가 원하는 특정 목록만 가져오고 싶을 경우 model을 정의하지 않고 get_queryset() method를 이용해서 특정 list만 반환하면 된다.
# reservations/views.py from django.shortcuts import redirect, reverse from django.views.generic import ListView from django.contrib import messages from lists import models as list_models from users import mixins class FavsList(mixins.LoggedInOnlyView, ListView): """ Fav List View Definition """ template_name = "pages/lists/fav_list.html" context_object_name = "rooms" paginate_by = 12 paginate_orphans = 6 ordering = "created" def get_queryset(self): the_list = list_models.List.objects.get_or_none(user=self.request.user) if the_list is None: messages.error(self.request, "List does not exist") return redirect(reverse("core:home")) return the_list.rooms.all() def get_context_data(self, **kwargs): page = int(self.request.GET.get("page", 1)) if page == "": page = 1 else: page = int(page) page_sector = (page - 1) // 5 page_sector = page_sector * 5 context = super().get_context_data() context["page_sector"] = page_sector return context
templates
pages/lists/fav_list.html
{% extends 'base.html' %} {% block page_title %} Favs {% 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 %} <div class="flex justify-center mt-20 itmes-center"> {% include 'mixins/page_number.html' with page=page_obj %} </div> </div> </div> {% endblock content %}
참고 자료
- 노마드 코더의 Airbnb 클론 강의
소스 코드
github.com/zpskek/airbnb-clone-v3/commit/84c38a06d35bd7acc3dc29580d37cb574dac743a
'Project using python > Cloning Airbnb' 카테고리의 다른 글
django conversations-list by FBV (0) 2021.04.23 django conversation-detail by CBV with DetailView (0) 2021.04.23 django conversations-detail by FBV (0) 2021.04.23 django create-conversation by FBV (0) 2021.04.23 django show-list by FBV (0) 2021.04.23