ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • django create-photo by FBV
    Project using python/Cloning Airbnb 2021. 3. 12. 09:11

    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("<int:pk>/photos/create/", views.createPhoto, name="create-photo")]
    

    FBV(Function Based View)

    rooms/views.py

    # rooms/views.py
    
    from django.shortcuts import render, redirect, reverse
    from django.contrib import messages
    from django.contrib.auth.decorators import login_required
    from django.http import Http404
    
    from . import models as room_models
    from photos import models as photo_models
    from users.exception import HostOnly
    
    @login_required
    def createPhoto(request, pk):
        if request.method == "GET":
            try:
                if not request.session.get("is_hosting"):
                    raise HostOnly("Page Not Found")
    
                room = room_models.Room.objects.get(pk=pk)
                if room is None:
                    raise Http404("Page not found")
    
                if request.user.pk != room.host.pk:
                    raise Http404("Page not found")
    
                return render(
                    request, "pages/rooms/photos/create_photo.html", {"room": room}
                )
            except HostOnly as error:
                messages.error(request, error)
                return redirect(reverse("core:home"))
        elif request.method == "POST":
            try:
                if not request.session.get("is_hosting"):
                    raise HostOnly("Page Not Found")
    
                room = room_models.Room.objects.get(pk=pk)
                if room is None:
                    raise Http404("Page not found")
    
                if request.user.pk != room.host.pk:
                    raise Http404("Page not found")
    
                caption = request.POST.get("caption")
                photo = request.FILES.get("photo")
                photo_models.Photo.objects.create(caption=caption, room=room, file=photo)
    
                messages.success(request, f"Create {caption}-photo successfully")
                return redirect(reverse("rooms:photo-list", kwargs={"pk": room.pk}))
            except HostOnly as error:
                messages.error(request, error)
                return redirect(reverse("core:home"))
    

    templates

    pages/rooms/photos/create_photo.html

    {% extends 'base.html' %}
    
    {% block page_title %}
      Create {{room.name}}'s Photo
    {% 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 Photo</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="caption" placeholder="Caption" required>
            </div>
            
            <div class="border p-3 border-gray-400 flex justify-between rounded-bl-lg rounded-br-lg">
              <input type="file" name="photo" class="p-1 w-full" accept="image/*">
            </div>
            <button class="form_button">Create</button>
          </form>
          <button class="button bg-gray-700">
            <a href="{% url 'rooms:room-detail' room.pk %}">Back</a>
          </button>
        </div>
      </div>
    {% endblock content %}

    참고 자료

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

    소스 코드

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

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

    django edit photo by FBV  (0) 2021.03.12
    django create-photo by CBV  (0) 2021.03.12
    django photo list by CBV  (0) 2021.03.11
    django photo list by FBV  (0) 2021.03.11
    django search page by FBV  (0) 2021.03.09

    댓글

Designed by Tistory.