-
django create-conversation by FBVProject using python/Cloning Airbnb 2021. 4. 23. 15:32
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( "<int:host_pk>/<int:guest_pk>/", views.createConversation, name="create-conversation", ), ]
Client
room_detail.html
<div class="flex flex-col"> <span class="mb-4">{% trans "Response rate" %} : 100%</span> <span class="mb-6">{% trans "Response time : In one hour" %}</span> <button class="button px-1 py-2 w-1/2"> <a href="{% url 'conversations:create-conversation' room.host.pk user.pk %}">Contact the host</a> </button> </div>
FBV(Function Based View)
conversations/views.py
from django.shortcuts import redirect, reverse from . import models as conversation_models from users import models as user_models def createConversation(request, host_pk, guest_pk): try: host = user_models.User.objects.get(pk=host_pk) guest = user_models.User.objects.get(pk=guest_pk) conversation = conversation_models.Conversation.objects.filter( participants=guest ).get(participants=host) return redirect( reverse("conversations:conversation-detail", kwargs={"pk": conversation.pk}) ) except conversation_models.Conversation.DoesNotExist: conversation = conversation_models.Conversation.objects.create() conversation.participants.add(host, guest) return redirect( reverse("conversations:conversation-detail", kwargs={"pk": conversation.pk}) )
참고 자료
- 노마드 코더의 Airbnb 클론 강의
소스 코드
github.com/zpskek/airbnb-clone-v3/commit/76e30bf6d1abad71c5213da936ea0dc957da9d70
'Project using python > Cloning Airbnb' 카테고리의 다른 글
django fav-list by CBV with ListView (0) 2021.04.23 django conversations-detail by FBV (0) 2021.04.23 django show-list by FBV (0) 2021.04.23 django toggle-list by FBV (0) 2021.04.23 django delete-review by FBV with axios (0) 2021.03.18