Project using python/Cloning Airbnb

django create-message by FBV

Cog Factory 2021. 4. 23. 17:19

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(
        "conversation-list/",
        views.conversationList,
        name="conversation-list",
    ),
]

templates

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="{% url 'conversations:create-message' conversation.pk %}" 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

# conversations/views.py

from django.shortcuts import redirect, reverse
from django.contrib import messages

from conversations import models as conversation_models


def createMessage(request, conversation_pk):
    message = request.POST.get("message")
    try:
        conversation = conversation_models.Conversation.objects.get(pk=conversation_pk)
        pk = []
        for participant in conversation.participants.all():
            pk.append(participant.pk)
        if message is not None:
            conversation_models.Message.objects.create(
                message=message, user=request.user, conversation=conversation
            )
    except conversation_models.Conversation.DoesNotExist:
        messages.error(request, "Conversation does not exist")
    return redirect(
        reverse("conversations:conversation-detail", kwargs={"pk": conversation.pk})
    )

참고 자료

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

소스 코드

github.com/zpskek/airbnb-clone-v3/commit/0ea875fa3a2e145675aeaf4a265413752cae4fee