-
django conversation-list by FBV with ListViewProject using python/Cloning Airbnb 2021. 4. 23. 17:05
url 설정
config/urls.py
urlpatterns = [ path("conversations/", include("conversations.urls", namespace="conversations")), ]
conversations/urls.py
from django.urls import path from . import views app_name = "conversations" urlpatterns = [ path( "conversation-list/", views.ConversationList.as_view(), name="conversation-list", ), ]
templates
pages/conversations/conversation_host_list.html
{% extends 'base.html' %} {% block page_title %} {{request.user}}'s Conversation List {% endblock page_title %} {% block content %} <div class="flex flex-col items-center justify-items-center"> <div class="w-10/12"> <div class="grid grid-cols-4 gap-4"> {% for conversation in conversations %} <a href="{% url 'conversations:conversation-detail' conversation.pk %}" class="border border-gray-500 p-6 rounded-lg flex justify-center items-center"> {% include 'mixins/conversation_avatars.html' with conversation=conversation %} </a> {% endfor %} </div> <div class="flex justify-center mt-20 itmes-center"> {% include 'mixins/page_number.html' with page=rooms %} </div> </div> </div> {% endblock content %}
CBV(Class Based View)
conversations/views.py
CBV는 django에서 정의한 여러가지 View들을 이용한다. 그 중 DetailView를 이용해서 conversations-detail page를 작성한다.
# conversations/views.py from django.views.generic import ListView from conversations import models as conversation_models from users import mixins class ConversationList(mixins.LoggedInOnlyView, ListView): """ Conversation List View Definition """ template_name = "pages/conversations/conversation_host_list.html" context_object_name = "conversations" paginate_by = 12 paginate_orphans = 6 ordering = "created" def get_queryset(self): return conversation_models.Conversation.objects.filter( participants=self.request.user ) 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
참고 자료
- 노마드 코더의 Airbnb 클론 강의
소스 코드
github.com/zpskek/airbnb-clone-v3/commit/fd430767d028f789dd3bf19b6837133bfcfd1fb5
'Project using python > Cloning Airbnb' 카테고리의 다른 글
Deploy django app on AWS EC2 (0) 2021.04.24 django create-message by FBV (0) 2021.04.23 django conversations-list by FBV (0) 2021.04.23 django conversation-detail by CBV with DetailView (0) 2021.04.23 django fav-list by CBV with ListView (0) 2021.04.23