๐ฅ Swich Btn for Session
๐ฅ Create Room in Host Mode
๐ ์ด ํฌ์คํ ์์๋ Django์ Session์ผ๋ก ์ฌ์ฉ์์ ๋ชจ๋(Guest/Host) ์ ํ์ ๊ตฌํํด๋ณด๊ฒ ์ต๋๋ค.
๋ชจ๋ ์ ๋ณด๋ฅผ DB์ ์ ์ฅํ ํ์๋ ์์ต๋๋ค. Guest Mode๋ Host Mode์ ๊ฒฝ์ฐ Session์ ์ฌ์ฉํ๋๊ฒ ๋ ๊ฐํธํฉ๋๋ค. Session์ Django์ Back๋จ์์ ์ ๋ณด๋ฅผ ์ ์ ์ ์งํ๋ค๊ฐ Logoutํ ๊ฒฝ์ฐ ๋ชจ๋ ์ฌ๋ผ์ง๋๋ค. ๋ฐ๋ผ์, ํํ์ ๋ณด(currency)์ฒ๋ผ ์ผ์์ ์ผ๋ก ํ์ํ ์ ๋ณด๋ค์ ์ฌ์ค Model์ field๋ฅผ ๋ง๋ค์ด ์ ์ฅํ๋ ๊ฒ ๋ณด๋ค Session์ ์ด์ฉํ๋๊ฒ ๋ ์ ์ ํฉ๋๋ค.!
"users/views.py"์์ session์ ํ์ฉํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค. ์ฐ์ session์ login๋ ์ํ์ฌ์ผํ๊ธฐ ๋๋ฌธ์ session์ ์ฌ๋ฆฌ๊ฑฐ๋ ๋ด๋ฆด ๋ ๋ชจ๋ login์ด ํ์์ ๋๋ค. ๋ฐ๋ผ์ "login_required" ๋ฐ์ฝ๋ ์ด์ ์ ์ฌ์ฉํฉ๋๋ค.
Host ๋ชจ๋๊ฐ ์์ฒญ๋๋ฉด "start_hosting" ํจ์๊ฐ ํธ์ถ๋๊ณ , ๋ค์ Guest ๋ชจ๋๋ฅผ ์์ฒญํ๋ฉด "stop_hosting" ํจ์๋ฅผ ์๋์ํต๋๋ค.
# users/views.py from django.contrib.auth.decorators import login_required # ๐ "login_required" import ... ... @login_required def start_hosting(request): request.session["is_hosting"] = True # ๐ session์ "is_hosting" ์ถ๊ฐ return redirect(reverse("core:home")) @login_required def stop_hosting(request): try: del request.session["is_hosting"] # ๐ session์์ "is_hosting" ์ ๊ฑฐ except KeyError: pass return redirect(reverse("core:home"))
์์ session ์ ํ ๊ธฐ๋ฅ์ ํ๋์ ํจ์๋ก ๊ธฐ๋ฅํ๊ฒ ํตํฉ์ํฌ ์ ์์ต๋๋ค. ์๋์ฒ๋ผ ๋ ๊ฐ์ ๊ฒฝ์ฐ๋ฐ์ ์์ ๋์๋ try ๊ตฌ๋ฌธ์ผ๋ก ์ ํ์์ผ์ค ์ ์์ต๋๋ค. ์ฆ, session์์ "is_hosting"๋ฅผ ์ ๊ฑฐํ๋ ค๊ณ ์๋ํ๊ณ , KeyError๊ฐ ๋ฐ์ํ๋ฉด session์ "is_hosting"์ด ์๊ธฐ ๋๋ฌธ์ ์ถ๊ฐํ๋ ๊ฒ์ด์ฃ . ์ด ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ฉด, url ๊ฒฝ๋ก๋ฅผ 1๊ฐ๋ง ์ง์ ํ๋ฉด ๋๋ค๋ ์ฅ์ ์ด ์์ต๋๋ค!
from django.contrib.auth.decorators import login_required # ๐ "login_required" import ... ... @login_required def switch_hosting(request): try: del request.session["is_hosting"] # ๐ ์ฐ์ ์ ๊ฑฐ๋ฅผ ์๋ํ๊ณ , except KeyError: request.session["is_hosting"] = True # ๐ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ฉด ์ถ๊ฐ return redirect(reverse("core:home"))
from django.urls import path from . import views app_name = "users" urlpatterns = [ ... ... ... path("switch-hosting/", views.switch_hosting, name="switch-hosting"), # ๐ swaich-hosting ]
์ด์ View์ Url์ ๋งคํํ์์ผ๋, Template์์ ์ฌ์ฉ์๊ฐ ํด๋ฆญํ ๋ฒํผ์ "partials/nav.html"์ ์์ฑํ๊ฒ ์ต๋๋ค. ๋ฒํผ์ swich๋๊ธฐ ๋๋ฌธ์ session์ด ์ถ๊ฐ๋ ์ํ์ผ ๋๋ Host ๋ชจ๋ ์ํ์ด๊ธฐ ๋๋ฌธ์ "Stop hosting" ๋ฒํผ์ด ๋ํ๋์ผํ๊ณ , ์ถ๊ฐ๋์ง ์์ ์ํ์์๋ "Start hosting" ๋ฒํผ์ด ๋ํ๋์ผ ํฉ๋๋ค.
์ด์ ํจ๊ป Host ๋ชจ๋์ผ ๋๋ ๊ฐ์ค์ ์์ฑํ ์ ์๋ "Create Room" ๋ฒํผ์ด ๋ํ๋๋๋ก ํ๊ฒ ์ต๋๋ค.
<ul class="flex items-center text-sm font-medium h-full"> {% if user.is_authenticated %} <li class="nav_link"> # ๐ session ์ ํ Btn <a href="{% url 'users:switch-hosting' %}"> {% if request.session.is_hosting %} # ๐ session์ "is_hosting"์ด ์กด์ฌํ๋ค๋ฉด,, Stop hosting {% else %} # ๐ session์ "is_hosting"์ด ์กด์ฌํ์ง ์๋๋ค๋ฉด,, Start hosting {% endif %} </a> </li> {% if request.session.is_hosting %} # ๐ host mode์ผ ๋, "Create Room" bnt ๋ ธ์ถ <li class="nav_link"><a href="#">Create Room</a></li> {% endif %} <li class="nav_link"><a href="{{user.get_absolute_url}}">Profile</a></li> <li class="nav_link"><a href="{% url "users:logout" %}">Log out</a></li> {% else %} <li class="nav_link"><a href="{% url "users:login" %}">Log in</a></li> <li class="nav_link"><a href="{% url "users:signup" %}">Sign up</a></li> {% endif %} </ul>
Host ๋ชจ๋์ผ ๋๋ง ๊ฐ์ค์ ์์ฑํ ์ ์๋ ๋ฒํผ์ ๋ ธ์ถํ ์ ์๊ฒ ํ์ผ๋ ์ด๋ฅผ ๊ธฐ๋ฅํ ์ ์๊ฒ ์ฒ๋ฆฌํด๋ณผ๊ป์. CBV๋ก ๋ง๋ค ๋, save() ๋งค์๋๋ฅผ ๊ฐ๋ก์ฑ์ผํ ๋๋ CreateView ๋ณด๋ค FormView ๋ง๋๋ ๊ฒ์ด ์ข์ต๋๋ค.
class CreateRoomView(user_mixins.LoggedInOnlyView, FormView): form_class = forms.CreateRoomForm # ๐ ํ ํ๋ฆฟ์ ์ ๋ ฅ์ ๋ฐ๊ธฐ ์ํ form์ ๋๋ค. template_name = "rooms/room_create.html" # ๐ ๊ฐ์ค์ ์์ฑํ๊ธฐ ์ํด renderํ ํ ํ๋ฆฟ์ ๋๋ค.
"room/forms.py"์ "CreateRoomForm"์ ModelForm์ ์์๋ฐ์, ๊ฐ์ค ์์ฑ์ ์ํ field๋ฅผ ์ง์ ํด์ค๋๋ค.
class CreateRoomForm(forms.ModelForm): class Meta: model = models.Room fields = ( "name", "description", "country", "city", "price", "address", "guests", "beds", "bedrooms", "baths", "check_in", "check_out", "instant_book", "room_type", "amenities", "facilities", "house_rule", )
"CreateRoomView"๋ฅผ ์๋์ํฌ Url ๊ฒฝ๋ก๋ฅผ ๋งคํํ๊ฒ ์ต๋๋ค.
from django.urls import path from . import views app_name = "rooms" urlpatterns = [ path("create/", views.CreateRoomView.as_view(), name="create"), # ๐ CreateRoomView๋ฅผ ์ฐ๊ฒฐํด์ค๋๋ค. path("<int:pk>/", views.RoomDetail.as_view(), name="detail"), path("<int:pk>/edit/", views.EditRoomView.as_view(), name="edit"), path("<int:pk>/photos/", views.RoomPhotosView.as_view(), name="photos"), path("<int:pk>/photos/add", views.AddPhotoView.as_view(), name="add-photos"), path( "<int:room_pk>/photos/<int:photo_pk>/delete/", views.delete_photo, name="delete-photo", ), path( "<int:room_pk>/photos/<int:photo_pk>/edit/", views.EditPhotoView.as_view(), name="edit-photo", ), path("search/", views.SearchView.as_view(), name="search"), ]
<ul class="flex items-center text-sm font-medium h-full"> {% if user.is_authenticated %} <li class="nav_link"> <a href="{% url 'users:switch-hosting' %}"> {% if request.session.is_hosting %} Stop hosting {% else %} Start hosting {% endif %} </a> </li> {% if request.session.is_hosting %} # ๐ url์ ์ฐ๊ฒฐํด์ค๊ป์:) <li class="nav_link"><a href="{% url 'rooms:create' %}">Create Room</a></li> {% endif %} <li class="nav_link"><a href="{{user.get_absolute_url}}">Profile</a></li> <li class="nav_link"><a href="{% url "users:logout" %}">Log out</a></li> {% else %} <li class="nav_link"><a href="{% url "users:login" %}">Log in</a></li> <li class="nav_link"><a href="{% url "users:signup" %}">Sign up</a></li> {% endif %} </ul>
์ด์ form์์ Room Object๋ฅผ ์์ฑ ํ,, ์ ์ฅํ๊ธฐ ์ ๊ฐ๋ก์ฑ์ ์์ฑ๋ Object๋ฅผ View๋จ์ผ๋ก ์ ๋ฌํด๋ณด๊ฒ ์ต๋๋ค.
class CreateRoomForm(forms.ModelForm): class Meta: model = models.Room fields = ( "name", "description", "country", "city", "price", "address", "guests", "beds", "bedrooms", "baths", "check_in", "check_out", "instant_book", "room_type", "amenities", "facilities", "house_rule", ) def save(self, *args, **kwargs): room = super().save(commit=False) # ๐ Object๋ฅผ ์์ฑ๊น์ง๋ง ํฉ๋๋ค. return room # ๐ ๊ทธ Object๋ฅผ View๋ก returnํฉ๋๋ค.
FBV์์๋ is_valid()๋ฅผ ์ฌ์ฉํ์ง๋ง, CBV์์๋ form_vaild๋ฅผ ์ฌ์ฉํด ์ ํจ์ฑ ๊ฒ์ฌ๋ฅผ ์งํ์ํต๋๋ค. ์ ํจํ๋ค๋ฉด, save ๋งค์๋๋ฅผ ํธ์ถํ์ฌ "CreateRoomForm"์์ Object์์ฑ ํ ์ ์ฅํ๊ธฐ ์ ์ํ๋ฅผ ๋ฐ์์ต๋๋ค. ์ดํ ํ์ฌ ์ฌ์ฉ์๋ฅผ host๋ก ์ง์ ํ ๋ค, ์ ์ฅ์ํต๋๋ค.
๋ง์ง๋ง์ผ๋ก ์ ์ฅํ field๋ค ์ค Many-to-Mnay field๊ฐ ์๋ค๋ฉด,, save_m2m ๋งค์๋๋ฅผ ์ด์ฉํด์ ์ ์ฅ์ํต๋๋ค.
class CreateRoomView(user_mixins.LoggedInOnlyView, FormView): form_class = forms.CreateRoomForm template_name = "rooms/room_create.html" def form_valid(self, form): room = form.save() # ๐ Form์์ ์์ฑ๋ Object ๊ฐ์ ธ์์:) room.host = self.request.user # ๐ ํ์ฌ ์ฌ์ฉ์๋ฅผ host๋ก ์ง์ ํฉ๋๋ค. room.save() # ๐ ์ ์ฅ. form.save_m2m() # ๐ Many-to-Many ํ๋๋ฅผ ์ ์ฅํด์ค์:) messages.success(self.request, "Room Uploaded") return redirect(reverse("rooms:detail", kwargs={"pk": room.pk})) # ๐ ์์ฑํ ๋ฐฉ์ผ๋ก rediect