-
django delete-photo by FBVProject using python/Cloning Airbnb 2021. 3. 12. 11:47
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:room_pk>/photos/<int:photo_pk>/delete/", views.deletePhoto, name="delete-photo", )]
FBV(Function Based View)
rooms/views.py
# rooms/views.py from django.shortcuts import render, redirect, reverse from django.contrib import messages from django.contrib.auth.decorators import login_required from django.http import Http404 from photos import models as photo_models from users.exception import HostOnly @login_required def deletePhoto(request, room_pk, photo_pk): try: if not request.session.get("is_hosting"): raise HostOnly("Page Not Found") photo = photo_models.Photo.objects.get_or_none(pk=photo_pk) if photo is None: messages.error(request, "Photo does not exsit") return redirect(reverse("core:home")) if request.user.pk != photo.room.host.pk: raise Http404("Page Not Found") photo.delete() messages.success(request, f"Delete {photo} successfully") return redirect(reverse("rooms:photo-list", kwargs={"pk": room_pk})) except HostOnly as error: messages.error(request, error) return redirect(reverse("core:home"))
참고 자료
- 노마드 코더의 Airbnb 클론 강의
소스 코드
github.com/zpskek/airbnb-clone-v3/commit/10dacd7153983f8ec2d015086ccd18073f030073
'Project using python > Cloning Airbnb' 카테고리의 다른 글
django reservation-list by FBV (0) 2021.03.15 django 'reserve a room' by FBV (0) 2021.03.13 django edit-photo by CBV (0) 2021.03.12 django edit photo by FBV (0) 2021.03.12 django create-photo by CBV (0) 2021.03.12