ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • django toggle-list by FBV
    Project using python/Cloning Airbnb 2021. 4. 23. 14:11

    url 설정

    config/urls.py

    urlpatterns = [
        path("lists/", include("lists.urls", namespace="lists"))
    ]

    lists/urls.py

    from django.urls import path
    from lists import views
    
    app_name = "lists"
    
    urlpatterns = [
        path("<int:room_pk>/toggle-list/", views.toggleList, name="toggle-list"),
    ]

    template tags

    on_favs

    원하는 방을 나의 찜 목록에 추가 했는지의 여부를 판단한다.

    from django import template
    from lists import models as list_models
    
    register = template.Library()
    
    
    @register.simple_tag(takes_context=True)
    def on_favs(context, room):
        user = context.request.user
        if str(user) == "AnonymousUser":
            return False
        try:
            the_list = list_models.List.objects.get(user=user, name="My Favorite Houses")
            return room in the_list.rooms.all()
        except list_models.List.DoesNotExist:
            return False

    Client

    review.html

    {% load on_favs %}
    
    <ul class="grid grid-cols-4 grid-rows-2-300px auto-rows-300px gap-4 gap-y-6">
      {% for room in rooms %}
        <li class="w-full relative">
          {% if url_page == 'reservations:host-room-list' %}
            <a href="{% url url_page request.user.pk room.pk %}">
          {% else %}
            <a href="{% url 'rooms:room-detail' room.pk %}">
          {% endif %}
            <div class="rounded-lg bg-cover bg-center h-3/4 mb-2" style="background-image:url('{{room.get_first_photo}}')">
              <div class="flex items-center relative top-4 {% if room.host.superhost %}justify-between{% else %}justify-end{% endif %}">
                {% if room.host.superhost %}
                  <span class="bg-white p-2 ml-3 rounded-lg font-semibold justify-start">Superhost</span>
                {% endif %}
                
              </div>
            </div>
            <div class="h-1/4">
              <div class="mb-1">
                <i class="fas fa-star text-red-500"></i>
                <span class="review-points">{{room.get_review_points}}</span>
                <span class="text-gray-400">({{room.reviews.count}})</span>
              </div>
              <div class="truncate">
                {{room.country}} &bull; {{room.address}}
              </div>
            </div>
          </a>
          {% on_favs room as on_favs_boolean %}
          {% if on_favs_boolean %}
            <a href="{% url 'lists:toggle-list' room.pk %}?action=remove" class="mr-3 absolute top-5 right-2">
              <i class="fas text-red-500 fa-heart  text-2xl justify-end text-center"></i>
            </a>
          {% else %}
            <a href="{% url 'lists:toggle-list' room.pk %}?action=add" class="mr-3 absolute top-5 right-2">
              <i class="far fa-heart  text-2xl text-white justify-end text-center"></i>
            </a>
          {% endif %}
        </li>
      {% endfor %}
    </ul>

     

    FBV(Function Based View)

    reviews/views.py

    # lists/views.py
    
    from django.shortcuts import redirect, reverse
    from . import models as list_models
    from rooms import models as room_models
    
    
    def toggleList(request, room_pk):
        user = request.user
        if str(user) == "AnonymousUser":
            return redirect(reverse("users:login"))
        action = request.GET.get("action")
        room = room_models.Room.objects.get(pk=room_pk)
        if room is not None and action is not None:
            the_list, b = list_models.List.objects.get_or_create(
                user=request.user, name="My Favorite Houses"
            )
            if action == "add":
                the_list.rooms.add(room)
            elif action == "remove":
                the_list.rooms.remove(room)
        return redirect(reverse("core:home"))
    

    참고 자료

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

    소스 코드

    github.com/zpskek/airbnb-clone-v3/commit/3998f5c0ada0e23e0ed5302e5fd7923912aefa91

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

    django create-conversation by FBV  (0) 2021.04.23
    django show-list by FBV  (0) 2021.04.23
    django delete-review by FBV with axios  (0) 2021.03.18
    django update-review with axios  (0) 2021.03.18
    django create-review by FBV  (0) 2021.03.18

    댓글

Designed by Tistory.