-
django create room by FBVProject using python/Cloning Airbnb 2021. 3. 8. 15:21
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("create/", views.creatRoom, name="create-room")]
FBV(Function Based View)
rooms/views.py
# rooms/views.py from django.shortcuts import render, redirect, reverse from django.contrib import messages from . import models as room_models from photos import models as photo_models @login_required def creatRoom(request): if request.method == "GET": if not request.session.get("is_hosting"): raise Http404("Page Not Found") room_types = room_models.RoomType.objects.all() amenities = room_models.Amenity.objects.all() facilities = room_models.Facility.objects.all() house_rules = room_models.HouseRule.objects.all() form = { "countries": countries, "room_types": room_types, "amenities": amenities, "facilities": facilities, "house_rules": house_rules, } return render(request, "pages/rooms/create_room.html", {**form}) elif request.method == "POST": if not request.session.get("is_hosting"): raise Http404("Page Not Found") host = request.user 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") caption = request.POST.get("caption") photo = request.FILES.get("photo") instant_book = bool(request.POST.get("instant_book")) room = room_models.Room.objects.create( name=name, city=city, address=address, price=price, guests=guests, bedrooms=bedrooms, beds=beds, bathrooms=bathrooms, description=description, host=host, room_type_id=room_type, instant_book=instant_book, ) room.country = country_code room.amenities.set(amenities) room.facilities.set(facilities) room.house_rules.set(house_rules) room.save() photo = photo_models.Photo.objects.create( file=photo, caption=caption, room_id=room.pk ) messages.success(request, f"Create {room.name} successfully") return redirect(reverse("rooms:room-detail", kwargs={"pk": room.pk}))
templates
pages/rooms/create_room.html
{% extends 'base.html' %} {% block page_title %} Create {{user.first_name}}'s Room {% 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">Create Room</h1> <form method="post" class="form" enctype="multipart/form-data"> {% csrf_token %} <div class="form_input rounded-tr-lg rounded-tl-lg"> <input type="text" name="name" placeholder="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}}">{{country.name}}</option> {% endfor %} </select> </div> <div class="form_input"> <input type="text" name="city" class="w-full" placeholder="City" required> </div> <div class="form_input"> <input type="text" name="address" class="w-full" placeholder="Address" required> </div> <div class="number_input"> <input type="number" name="price" class="w-full" placeholder="Price" min=0 required> </div> <div class="number_input"> <input type="number" name="guests"class="w-full" placeholder="Guests" min=0 required> </div> <div class="number_input"> <input type="number" name="bedrooms" class="w-full" placeholder="Bedrooms" min=0 required> </div> <div class="number_input"> <input type="number" name="beds" class="w-full" placeholder="Beds" min=0 required> </div> <div class="number_input"> <input type="number" 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}}"> {{room_type}}</option> {% endfor %} </select> </div> <div class="form_input"> <textarea name="description" class="h-40" placeholder="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}}" > </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}}" > </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}}" > </li> {% endfor %} </ul> </div> <div class="border p-3 border-gray-400 flex justify-between"> <input type="text" name="caption" class="p-1 w-full" placeholder="Caption"> <input type="file" name="photo" class="p-1 w-full"> </div> <div class="checkbox rounded-bl-lg rounded-br-lg"> <label for="instant_book" class="text-lg font-bold mr-2">Instant Book</label> <input type="checkbox", name="instant_book" id="instant_book"> </div> <button class="form_button">Create</button> </form> </div> </div> {% endblock content %}
참고 자료
- 노마드 코더의 Airbnb 클론 강의
소스 코드
github.com/zpskek/airbnb-clone-v3/commit/9045b84603bdfcb5f469cc59ea8e5b1c269f564a
'Project using python > Cloning Airbnb' 카테고리의 다른 글
django edit room by FBV (0) 2021.03.09 django create room by CBV (0) 2021.03.08 django Http404() (0) 2021.03.08 django translator (0) 2021.03.07 django session (0) 2021.03.06