From 73fc2d41ee8c4d4cc36bb8c0eefc2b0e33e44509 Mon Sep 17 00:00:00 2001 From: Yuriy Komar Date: Wed, 11 Dec 2024 18:17:14 +0200 Subject: [PATCH 1/2] 'Solution' --- taxi/urls.py | 25 ++++++++++++++ taxi/views.py | 41 +++++++++++++++++++++++ taxi_service/settings.py | 6 ++++ templates/includes/pagination.html | 3 +- templates/taxi/car_form.html | 15 +++++++++ templates/taxi/car_list.html | 40 +++++++++++++++++----- templates/taxi/car_manufacturer_form.html | 15 +++++++++ templates/taxi/confirm_delete.html | 17 ++++++++++ templates/taxi/manufacturer_list.html | 15 +++++++-- 9 files changed, 166 insertions(+), 11 deletions(-) create mode 100644 templates/taxi/car_form.html create mode 100644 templates/taxi/car_manufacturer_form.html create mode 100644 templates/taxi/confirm_delete.html diff --git a/taxi/urls.py b/taxi/urls.py index c663d6e22..af099f19e 100644 --- a/taxi/urls.py +++ b/taxi/urls.py @@ -4,11 +4,18 @@ index, CarListView, CarDetailView, + CarCreateView, + CarUpdateView, + CarDeleteView, DriverListView, DriverDetailView, ManufacturerListView, + ManufacturerCreateView, + ManufacturerUpdateView, + ManufacturerDeleteView, ) + urlpatterns = [ path("", index, name="index"), path( @@ -16,8 +23,26 @@ ManufacturerListView.as_view(), name="manufacturer-list", ), + path( + "manufacturers/create", + ManufacturerCreateView.as_view(), + name="manufacturer-create", + ), + path( + "manufacturers/update//", + ManufacturerUpdateView.as_view(), + name="manufacturer-update", + ), + path( + "manufacturers/delete//", + ManufacturerDeleteView.as_view(), + name="manufacturer-delete", + ), path("cars/", CarListView.as_view(), name="car-list"), path("cars//", CarDetailView.as_view(), name="car-detail"), + path("cars/create/", CarCreateView.as_view(), name="car-create"), + path("cars/update//", CarUpdateView.as_view(), name="car-update"), + path("cars/delete//", CarDeleteView.as_view(), name="car-delete"), path("drivers/", DriverListView.as_view(), name="driver-list"), path( "drivers//", DriverDetailView.as_view(), name="driver-detail" diff --git a/taxi/views.py b/taxi/views.py index 4a99a2cd9..011da4efe 100644 --- a/taxi/views.py +++ b/taxi/views.py @@ -1,5 +1,6 @@ from django.contrib.auth.decorators import login_required from django.shortcuts import render +from django.urls import reverse_lazy from django.views import generic from django.contrib.auth.mixins import LoginRequiredMixin @@ -34,12 +35,52 @@ class ManufacturerListView(LoginRequiredMixin, generic.ListView): paginate_by = 5 +class ManufacturerCreateView(LoginRequiredMixin, generic.CreateView): + model = Manufacturer + fields = "__all__" + template_name = "taxi/car_manufacturer_form.html" + success_url = reverse_lazy("taxi:manufacturer-list") + + +class ManufacturerUpdateView(LoginRequiredMixin, generic.UpdateView): + model = Manufacturer + fields = "__all__" + template_name = "taxi/car_manufacturer_form.html" + success_url = reverse_lazy("taxi:manufacturer-list") + + +class ManufacturerDeleteView(LoginRequiredMixin, generic.DeleteView): + model = Manufacturer + template_name = "taxi/confirm_delete.html" + success_url = reverse_lazy("taxi:manufacturer-list") + + class CarListView(LoginRequiredMixin, generic.ListView): model = Car paginate_by = 5 queryset = Car.objects.all().select_related("manufacturer") +class CarCreateView(LoginRequiredMixin, generic.CreateView): + model = Car + fields = "__all__" + template_name = "taxi/car_form.html" + success_url = reverse_lazy("taxi:car-list") + + +class CarUpdateView(LoginRequiredMixin, generic.UpdateView): + model = Car + fields = "__all__" + template_name = "taxi/car_form.html" + success_url = reverse_lazy("taxi:car-list") + + +class CarDeleteView(LoginRequiredMixin, generic.DeleteView): + model = Car + template_name = "taxi/confirm_delete.html" + success_url = reverse_lazy("taxi:car-list") + + class CarDetailView(LoginRequiredMixin, generic.DetailView): model = Car diff --git a/taxi_service/settings.py b/taxi_service/settings.py index d89c45643..ad875ea87 100644 --- a/taxi_service/settings.py +++ b/taxi_service/settings.py @@ -43,7 +43,10 @@ "django.contrib.messages", "django.contrib.staticfiles", "debug_toolbar", + "crispy_forms", + "crispy_bootstrap4", "taxi", + ] MIDDLEWARE = [ @@ -140,3 +143,6 @@ # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" + +CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap4" +CRISPY_TEMPLATE_PACK = "bootstrap4" diff --git a/templates/includes/pagination.html b/templates/includes/pagination.html index 118c2806e..e9c950eaa 100644 --- a/templates/includes/pagination.html +++ b/templates/includes/pagination.html @@ -1,5 +1,6 @@ {% if is_paginated %} -
    +
    +
      {% if page_obj.has_previous %}
    • prev diff --git a/templates/taxi/car_form.html b/templates/taxi/car_form.html new file mode 100644 index 000000000..7e99ec768 --- /dev/null +++ b/templates/taxi/car_form.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} + +{% load crispy_forms_filters %} +{% block content %} +

      {{ object|yesno:"Update, Create new" }} Car

      +
      +
      +
      + {% csrf_token %} + {{ form|crispy }} + +
      +
      + +{% endblock %} diff --git a/templates/taxi/car_list.html b/templates/taxi/car_list.html index e107b5797..5296f1f75 100644 --- a/templates/taxi/car_list.html +++ b/templates/taxi/car_list.html @@ -1,17 +1,41 @@ {% extends "base.html" %} {% block content %} -

      Car list

      +

      + Cars + Add new Car +

      +

      List of our fleet. The best brands, proven and reliable models.

      {% if car_list %} -
        + + + + + + + + + + {% for car in car_list %} -
      • - {{ car.id }} - {{ car.model }} ({{ car.manufacturer.name }}) -
      • + + + + + + {% endfor %} - + +
        #ModelManufacturerModify
        {{ car.model }} {{ car.manufacturer }} + Update + Delete +
        {% else %} -

        There are no cars in taxi

        +

        No cars found.

        {% endif %} +
        + {% endblock %} diff --git a/templates/taxi/car_manufacturer_form.html b/templates/taxi/car_manufacturer_form.html new file mode 100644 index 000000000..d83f99e6e --- /dev/null +++ b/templates/taxi/car_manufacturer_form.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} + +{% load crispy_forms_filters %} +{% block content %} +

        {{ object|yesno:"Update, Create new" }} Manufacturer

        +
        +
        +
        + {% csrf_token %} + {{ form|crispy }} + +
        +
        + +{% endblock %} diff --git a/templates/taxi/confirm_delete.html b/templates/taxi/confirm_delete.html new file mode 100644 index 000000000..f931e09bd --- /dev/null +++ b/templates/taxi/confirm_delete.html @@ -0,0 +1,17 @@ +{% extends "base.html" %} + +{% load crispy_forms_filters %} + +{% block content %} +

        Delete {{ car|yesno:"Car, Manufacturer" }}

        +
        +
        +

        Are you sure you want to delete "{{ object }}"?

        +
        + {% csrf_token %} + {{ form|crispy }} + +
        +
        + +{% endblock %} diff --git a/templates/taxi/manufacturer_list.html b/templates/taxi/manufacturer_list.html index 2a31bcf60..67d62fb06 100644 --- a/templates/taxi/manufacturer_list.html +++ b/templates/taxi/manufacturer_list.html @@ -1,15 +1,18 @@ {% extends "base.html" %} {% block content %} -

        Manufacturer List +

        + Manufacturer List + Add new Manufacturer

        - + {% if manufacturer_list %} + {% for manufacturer in manufacturer_list %} @@ -23,6 +26,10 @@

        Manufacturer List

        + {% endfor %}
        ID Name CountryModify
        {{ manufacturer.country }} + Update + Delete +
        @@ -30,4 +37,8 @@

        Manufacturer List {% else %}

        There are no manufacturers in the service.

        {% endif %} + {% endblock %} From 2076bff85d3dadd609b59d1b961fd2ce97945780 Mon Sep 17 00:00:00 2001 From: Yuriy Komar Date: Wed, 11 Dec 2024 18:30:07 +0200 Subject: [PATCH 2/2] 'Solution2' --- taxi/urls.py | 2 +- templates/includes/pagination.html | 4 ++-- templates/taxi/car_list.html | 4 ++-- templates/taxi/manufacturer_list.html | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/taxi/urls.py b/taxi/urls.py index af099f19e..e3863c0a7 100644 --- a/taxi/urls.py +++ b/taxi/urls.py @@ -24,7 +24,7 @@ name="manufacturer-list", ), path( - "manufacturers/create", + "manufacturers/create/", ManufacturerCreateView.as_view(), name="manufacturer-create", ), diff --git a/templates/includes/pagination.html b/templates/includes/pagination.html index e9c950eaa..d0ea4f7d6 100644 --- a/templates/includes/pagination.html +++ b/templates/includes/pagination.html @@ -3,7 +3,7 @@
          {% if page_obj.has_previous %}
        • - prev + prev
        • {% endif %}
        • @@ -11,7 +11,7 @@
        • {% if page_obj.has_next %}
        • - next + next
        • {% endif %}
        diff --git a/templates/taxi/car_list.html b/templates/taxi/car_list.html index 5296f1f75..262f0f5ab 100644 --- a/templates/taxi/car_list.html +++ b/templates/taxi/car_list.html @@ -23,8 +23,8 @@

        {{ car.model }} {{ car.manufacturer }} - Update - Delete + Update + Delete {% endfor %} diff --git a/templates/taxi/manufacturer_list.html b/templates/taxi/manufacturer_list.html index 67d62fb06..857097f55 100644 --- a/templates/taxi/manufacturer_list.html +++ b/templates/taxi/manufacturer_list.html @@ -27,8 +27,8 @@

        {{ manufacturer.country }} - Update - Delete + Update + Delete {% endfor %}