ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • django edit room by FBV
    Project using python/Cloning Airbnb 2021. 3. 9. 07:05

    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>/edit/", views.editRoom, name="edit-room")]
    

    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_countries import countries
    
    from . import models as room_models
    from users.exception import HostOnly, VerifyUser
    
    @login_required
    def editRoom(request, pk):
        if request.method == "GET":
            try:
                if not request.session.get("is_hosting"):
                    raise HostOnly("Page Not Found")
    
                room = room_models.Room.objects.get(pk=pk)
    
                if room is None:
                    messages.error(request, "Room does not exsit")
                    return redirect(reverse("core:home"))
    
                if request.user.pk != room.host.pk:
                    raise VerifyUser("Page Not Found")
    
                amenities = room_models.Amenity.objects.all()
                facilities = room_models.Facility.objects.all()
                house_rules = room_models.HouseRule.objects.all()
                room_types = room_models.RoomType.objects.all()
    
                s_amenities = room.amenities.all()
                s_facilities = room.facilities.all()
                s_house_rules = room.house_rules.all()
    
                form = {
                    "countries": countries,
                    "room_types": room_types,
                    "amenities": amenities,
                    "facilities": facilities,
                    "house_rules": house_rules,
                }
    
                choices = {
                    "s_amenities": s_amenities,
                    "s_facilities": s_facilities,
                    "s_house_rules": s_house_rules,
                }
    
                return render(
                    request, "pages/rooms/edit_room.html", {"room": room, **form, **choices}
                )
            except (HostOnly, VerifyUser) 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")
    
                room = room_models.Room.objects.get(pk=pk)
    
                if room is None:
                    messages.error(request, "Room does not exsit")
                    return redirect(reverse("core:home"))
    
                if request.user.pk != room.host.pk:
                    raise VerifyUser("Page Not Found")
    
                name = request.POST.get("name")
                city = request.POST.get("city")
                address = request.POST.get("address")
                country_code = request.POST.get("country")
                price = int(request.POST.get("price", 0))
                guests = int(request.POST.get("guests", 0))
                bedrooms = int(request.POST.get("bedrooms", 0))
                beds = int(request.POST.get("beds", 0))
                bathrooms = int(request.POST.get("bathrooms", 0))
                room_type = int(request.POST.get("room_type", 0))
                description = request.POST.get("description")
                amenities = request.POST.getlist("amenities")
                facilities = request.POST.getlist("facilities")
                house_rules = request.POST.getlist("house_rules")
                instant_book = bool(request.POST.get("instant_book"))
    
                s_room_type = room_models.RoomType.objects.get(pk=room_type)
    
                room.name = name
                room.city = city
                room.address = address
                room.price = price
                room.guests = guests
                room.bedrooms = bedrooms
                room.beds = beds
                room.bathrooms = bathrooms
                room.room_type = s_room_type
                room.description = description
                room.instant_book = instant_book
    
                room.country = country_code
                room.save()
    
                room.amenities.set(amenities)
                room.facilities.set(facilities)
                room.house_rules.set(house_rules)
    
                messages.success(request, f"Edit {room.name} successfully")
                return redirect(reverse("rooms:room-detail", kwargs={"pk": room.pk}))
            except (HostOnly, VerifyUser) as error:
                messages.error(request, error)
                return redirect(reverse("core:home"))

    templates

    pages/rooms/edit_room.html

    {% extends 'base.html' %}
    
    {% block page_title %}
      Edit {{room.name}}
    {% 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 Room</h1>
          <form method="post" class="form">
            {% csrf_token %}
            <div class="form_input rounded-tr-lg rounded-tl-lg">
              <input type="text" name="name" placeholder="Name" value={{room.name}} required>
            </div>
            <div class="select">
              <label for="country">Country</label>
              <select name="country" id="country" required>
                {% for country in countries %}
                  <option value="{{country.code}}" 
                    {% if country.code == room.country.code %}
                      selected
                    {% endif %}
                  >{{country.name}}</option>
                {% endfor %}
              </select>
            </div>
    
            <div class="form_input">
              <input type="text" value={{room.city}} name="city" class="w-full" placeholder="City" required>
            </div>
            
            <div class="form_input">
              <input type="text" value={{room.address}} name="address" class="w-full" placeholder="Address" required>
            </div>
    
            <div class="number_input">
              <input type="number" value={{room.price}} name="price" class="w-full" placeholder="Price" min=0 required>
            </div>
    
            <div class="number_input">
              <input type="number" value={{room.guests}} name="guests"class="w-full" placeholder="Guests" min=0 required>
            </div>
    
            <div class="number_input">
              <input type="number" value={{room.bedrooms}}  name="bedrooms" class="w-full" placeholder="Bedrooms" min=0 required>
            </div>
    
            <div class="number_input">
              <input type="number"value={{room.beds}}  name="beds" class="w-full" placeholder="Beds" min=0 required>
            </div>
    
            <div class="number_input">
              <input type="number"value={{room.bathrooms}}  name="bathrooms" min=0 class="w-full" placeholder="Bathrooms" required>
            </div>
            
            <div class="select">
              <label for="room_type" class="text-lg font-bold">Room Type</label>
              <select name="room_type" id="room_type" required>
                {% for room_type in room_types %}
                  <option value="{{room_type.pk}}" name="{{room_type.pk}}"
                  {% if room_type.pk == room.room_type.pk %}
                    selected
                  {% endif %}
                    >{{room_type}}</option>
                {% endfor %}
              </select>
            </div>
    
            <div class="form_input">
              <textarea name="description" class="h-40" placeholder="Description">{{room.description}}</textarea>
            </div>
    
            <div class="checkbox">
              <h3>Amenity</h3>
              <ul>
              {% for amenity in amenities %}
                <li>
                  <label for=amenity_{{amenity.pk}}>{{amenity.name}}</label>
                  <input
                    type="checkbox"
                    name="amenities"
                    value="{{amenity.pk}}"
                    id="amenity_{{amenity.pk}}"
                    {% if amenity in s_amenities %}
                      checked
                    {% endif %}
                    >
                </li>
              {% endfor %}
              </ul>
            </div>
    
            <div class="checkbox">
              <h3>Facility</h3>
    
              <ul>
              {% for facility in facilities %}
                <li>
                  <label for=facility_{{facility.pk}}>{{facility.name}}</label>
                  <input
                    type="checkbox"
                    name="facilities"
                    value="{{facility.pk}}"
                    id="facility_{{facility.pk}}"
                    {% if facility in s_facilities %}
                      checked
                    {% endif %}
                    >
                </li>
              {% endfor %}
              </ul>
            </div>
    
            <div class="checkbox">
              <h3>House Rules</h3>
              <ul>
              {% for house_rule in house_rules %}
                <li>
                  <label for=house_rule_{{house_rule.pk}}>{{house_rule.name}}</label>
                  <input
                    type="checkbox"
                    name="house_rules"
                    value="{{house_rule.pk}}"
                    id="house_rule_{{house_rule.pk}}"
                    {% if house_rule in s_house_rules %}
                      checked
                    {% endif %}
                    >
                </li>
              {% endfor %}
              </ul>
            </div>
            
            <div class="checkbox rounded-bl-lg rounded-br-lg mb-4">
              <label for="instant_book" class="text-lg font-bold mr-2">Instant Book</label>
              <input 
                type="checkbox"
                name="instant_book" 
                id="instant_book"
                {% if room.instant_book %}
                  checked
                {% endif %}
              >
            </div>
            <button class="form_button">Edit</button>
          </form>
          <button class="button">
            <a href="#">Photo Settings</a>
          </button>
          <button class="button bg-red-600">
            <a href="#">Delete Room</a>
          </button>
          <button class="button bg-gray-700">
            <a href="{% url 'rooms:room-detail' room.pk %}">Back</a>
          </button>
        </div>
      </div>
        
    {% endblock content %}

    참고 자료

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

    소스 코드

    github.com/zpskek/airbnb-clone-v3/commit/4b8ca793d253cf1af757ec142411cb8f3fad8524

    'Project using python > Cloning Airbnb' 카테고리의 다른 글

    django delete room by FBV  (0) 2021.03.09
    django edit room by CBV  (0) 2021.03.09
    django create room by CBV  (0) 2021.03.08
    django create room by FBV  (0) 2021.03.08
    django Http404()  (0) 2021.03.08

    댓글

Designed by Tistory.