Project using python/Cloning Airbnb

django edit photo by FBV

Cog Factory 2021. 3. 12. 10:31

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>/edit/",
        views.editPhoto,
        name="edit-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 . import models as room_models
from photos import models as photo_models
from users.exception import HostOnly

@login_required
def editPhoto(request, room_pk, photo_pk):
    if request.method == "GET":
        try:
            if not request.session.get("is_hosting"):
                raise HostOnly("Page Not Found")

            room = room_models.Room.objects.get_or_none(pk=room_pk)
            if room is None:
                messages.error(request, "Room does not exist")
                return redirect(reverse("core:home"))

            if request.user.pk != room.host.pk:
                raise Http404("Page Not Found")
            photo = room.photos.get(pk=photo_pk)

            return render(
                request,
                "pages/rooms/photos/edit_photo.html",
                context={"room": room, "photo": photo},
            )
        except HostOnly as error:
            messages.error(request, error)
            return redirect(reverse("core:home"))
    elif request.method == "POST":
        try:
            if not request.session.get("is_hosting"):
                raise HostOnly("Page Not Found")
            caption = request.POST.get("caption")

            room = room_models.Room.objects.get_or_none(pk=room_pk)
            photo = room.photos.get(pk=photo_pk)
            photo.caption = caption
            photo.save()

            messages.success(request, f"Edit {caption}-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"))

templates

pages/rooms/photos/edit_photo.html

{% extends 'base.html' %}

{% block page_title %}
  Edit {{room.name}}'s Photo
{% endblock page_title %}


{% block search-bar %}
<div></div>
{% endblock search-bar %}

{% block content %}
  <div class="background">
    <div class="wrap">
      <h1 class="text-3xl font-bold pb-6 pt-3 text-center">Edit Photo('{{photo}}')</h1>
      <form method="post" class="form" enctype="multipart/form-data">
        {% csrf_token %}
        <div class="form_input rounded-lg flex">
          <label for="caption">Caption:</label>
          <input type="text" id="caption" name="caption" placeholder="Caption" value="{{photo}}" required>
        </div>
        <button class="form_button">Edit</button>
      </form>
      <button class="button bg-gray-700">
        <a href="{% url 'rooms:photo-list' room.pk %}">Back</a>
      </button>
    </div>
  </div>
{% endblock content %}

참고 자료

  • 노마드 코더의 Airbnb 클론 강의

소스 코드

github.com/zpskek/airbnb-clone-v3/commit/b5c45e7392fe48c76a30673c1eb0b53b913f89df