-
django cancel reservation by FBVProject using python/Cloning Airbnb 2021. 3. 17. 10:22
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:pk>/cancel/", views.cancelReservation, name="cancel") ]
FBV(Function Based View)
reservations/views.py
# reservations/views.py from django.shortcuts import redirect, reverse from django.contrib.auth.decorators import login_required from django.contrib import messages from django.http import Http404 from reservations import models as reservation_models @login_required def cancelReservation(request, pk): reservation = reservation_models.Reservation.objects.get_or_none(pk=pk) if reservation is None: messages.error(request, "Rservation does not exist") return redirect(reverse("core:home")) if request.user != reservation.guest and request.user != reservation.room.host: raise Http404() room = reservation.room reservation.delete() return redirect(reverse("rooms:room-detail", kwargs={"pk": room.pk}))
참고 자료
- 노마드 코더의 Airbnb 클론 강의
소스 코드
github.com/zpskek/airbnb-clone-v3/commit/b7a257757030f19f9ce41889a0c6936b74b6d955
'Project using python > Cloning Airbnb' 카테고리의 다른 글
django update-review with axios (0) 2021.03.18 django create-review by FBV (0) 2021.03.18 django confirm reservation by FBV (0) 2021.03.17 django reservation-list on room in host by CBV with ListView (0) 2021.03.17 django reservation-list on room in host by FBV (0) 2021.03.17