-
django delete room by FBVProject using python/Cloning Airbnb 2021. 3. 9. 10:01
url 설정
config/urls.py
urlpatterns = [ path("rooms/", include("rooms.urls", namespace="room")) ]
rooms/urls.py
from django.urls import path from . import views app_name = "rooms" urlpatterns = [path("<int:pk>/delete/", views.deleteRoom, name="delete-room")]
FBV(Function Based View)
rooms/views.py
# rooms/views.py from django.shortcuts import redirect, reverse from django.contrib import messages from django.contrib.auth.decorators import login_required from django.http import Http404 from . import models as room_models from users.exception import HostOnly @login_required def deleteRoom(request, pk): try: if not request.session.get("is_hosting"): raise HostOnly("Change ot host mode") room = room_models.Room.objects.get_or_none(pk=pk) if request.user.pk != room.host.pk: raise Http404("Page Not Found") if room is None: messages.error(request, "Room does not exsit") return redirect(reverse("core:home")) room.delete() messages.success(request, f"Delete {room.name} successfully") return redirect(reverse("users:profile", kwargs={"pk": request.user.pk})) except HostOnly as error: messages.error(request, error) return redirect(reverse("core:home"))
소스 코드
github.com/zpskek/airbnb-clone-v3/commit/2ae3bd4e332abeb6a8b07e67b659c456f760d84e
'Project using python > Cloning Airbnb' 카테고리의 다른 글
django photo list by FBV (0) 2021.03.11 django search page by FBV (0) 2021.03.09 django edit room by CBV (0) 2021.03.09 django edit room by FBV (0) 2021.03.09 django create room by CBV (0) 2021.03.08