ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • django conversations-detail by FBV
    Project using python/Cloning Airbnb 2021. 4. 23. 15:38

    url 설정

    config/urls.py

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

    conversations/urls.py

    from django.urls import path
    from . import views
    
    
    app_name = "conversations"
    
    urlpatterns = [
        path(
            "<int:host_pk>/<int:guest_pk>/",
            views.createConversation,
            name="create-conversation",
        ),
        path(
            "<int:pk>/conversation-detail/",
            views.conversationDetail,
            name="conversation-detail",
        ),
    ]
    

    Client

    pages/conversations/conversation_detail.html

    {% extends 'base.html' %}
    
    {% load remainder_op %}
    
    {% block page_title %}
        Conversation-{{room}}
    {% endblock page_title %}
    
    {% block content %}
        <div class="flex flex-col items-center justify-items-center">
            <main class="border border-gray-500 p-6 rounded-lg w-1/2">
                <div class="flex items-center justify-center mb-10">
                    {% include 'mixins/conversation_avatars.html' with conversation=conversation %}
                </div>
                <div class="flex flex-col">
                    {% for message in conversation.messages.all %}
                        <div class=" {% if message.user.pk == user.pk %}self-end text-right{% endif %}">
                            <div class="mb-2 text-sm font-medium text-gray-600">{{message.user.first_name}}</div>
                            <div class="text-base mb-4 font-medium p-3 rounded-lg max-w-xs
                            {% if message.user.pk == user.pk %}
                                bg-green-500
                                text-white
                            {% else %}
                                bg-gray-300
                            {% endif %}
                            ">
                            {{message.message}}
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                    <div class="flex justify-center">
                    <form action="#" class="flex flex-col mt-10 rounded-2xl shadow-lg h-10 w-1/3" method="POST">
                        {% csrf_token %}
                        <input class="text-center outline-none" name="message" placeholder="Write a Message" required />
                    </form>
                </div>
            </main>
        </div>
    {% endblock content %}

    FBV(Function Based View)

    conversations/views.py

    from django.shortcuts import redirect, reverse
    from . import models as conversation_models
    from users import models as user_models
    
    
    def conversationDetail(request, pk):
        conversation = conversation_models.Conversation.objects.get(pk=pk)
    
        return render(
            request,
            "pages/conversations/conversation_detail.html",
            context={"conversation": conversation},
        )
    

    참고 자료

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

    소스 코드

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

    댓글

Designed by Tistory.