From aee3829db565b94b5290db4d005645bc62607c16 Mon Sep 17 00:00:00 2001 From: "o.makieieva" Date: Tue, 15 Aug 2023 11:46:34 +0200 Subject: [PATCH] py-taxi-service-class-based-views --- taxi/urls.py | 14 ++++++++- taxi/views.py | 41 +++++++++++++++++++++++++++ templates/base.html | 7 +++-- templates/includes/pagination.html | 20 +++++++++++++ templates/includes/sidebar.html | 8 +++--- templates/taxi/car_detail.html | 15 ++++++++++ templates/taxi/car_list.html | 22 ++++++++++++++ templates/taxi/driver_detail.html | 15 ++++++++++ templates/taxi/driver_list.html | 28 ++++++++++++++++++ templates/taxi/manufacturer_list.html | 22 ++++++++++++++ 10 files changed, 184 insertions(+), 8 deletions(-) create mode 100644 templates/includes/pagination.html create mode 100644 templates/taxi/car_detail.html create mode 100644 templates/taxi/car_list.html create mode 100644 templates/taxi/driver_detail.html create mode 100644 templates/taxi/driver_list.html create mode 100644 templates/taxi/manufacturer_list.html diff --git a/taxi/urls.py b/taxi/urls.py index d1911da6b..9501ec3c7 100644 --- a/taxi/urls.py +++ b/taxi/urls.py @@ -1,9 +1,21 @@ from django.urls import path -from .views import index +from .views import ( + index, ManufacturerListView, CarListView, + CarDetailView, DriverListView, DriverDetailView) urlpatterns = [ path("", index, name="index"), + path( + "manufacturer-list/", + ManufacturerListView.as_view(), + name="manufacturer-list" + ), + path("cars/", CarListView.as_view(), name="car-list"), + path("cars//", CarDetailView.as_view(), name="car-detail"), + path("drivers/", DriverListView.as_view(), name="driver-list"), + path("drivers//", DriverDetailView.as_view(), name="driver-detail") + ] app_name = "taxi" diff --git a/taxi/views.py b/taxi/views.py index 8e16b5565..8f7c75f69 100644 --- a/taxi/views.py +++ b/taxi/views.py @@ -1,4 +1,5 @@ from django.shortcuts import render +from django.views import generic from .models import Driver, Car, Manufacturer @@ -17,3 +18,43 @@ def index(request): } return render(request, "taxi/index.html", context=context) + + +class ManufacturerListView(generic.ListView): + model = Manufacturer + queryset = Manufacturer.objects.all().order_by("name") + template_name = "taxi/manufacturer_list.html" + paginate_by = 5 + + +class CarListView(generic.ListView): + model = Car + queryset = Car.objects.all().select_related("manufacturer") + template_name = "taxi/car_list.html" + paginate_by = 5 + + +class CarDetailView(generic.DetailView): + model = Car + template_name = "taxi/car_detail.html" + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context["drivers"] = self.object.drivers.all() + return context + + +class DriverListView(generic.ListView): + model = Driver + paginate_by = 5 + template_name = "taxi/driver_list.html" + + +class DriverDetailView(generic.DetailView): + model = Driver + template_name = "taxi/driver_detail.html" + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context["cars"] = self.object.cars.all() + return context diff --git a/templates/base.html b/templates/base.html index 272284d17..3e0343c9e 100644 --- a/templates/base.html +++ b/templates/base.html @@ -6,9 +6,9 @@ + href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" + integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" + crossorigin="anonymous"> {% load static %} @@ -29,6 +29,7 @@ {% block content %}{% endblock %} {% block pagination %} + {% include "includes/pagination.html" %} {% endblock %} diff --git a/templates/includes/pagination.html b/templates/includes/pagination.html new file mode 100644 index 000000000..41c825a8c --- /dev/null +++ b/templates/includes/pagination.html @@ -0,0 +1,20 @@ +{% if is_paginated %} +
    + {% if page_obj.has_previous %} +
  • + prev +
  • + {% endif %} +
  • + {{ page_obj.number }} of {{ paginator.num_pages }} +
  • + + {% if page_obj.has_next %} +
  • + next +
  • + + {% endif %} +
+ +{% endif %} diff --git a/templates/includes/sidebar.html b/templates/includes/sidebar.html index b42a684ac..e32a29f11 100644 --- a/templates/includes/sidebar.html +++ b/templates/includes/sidebar.html @@ -1,6 +1,6 @@ diff --git a/templates/taxi/car_detail.html b/templates/taxi/car_detail.html new file mode 100644 index 000000000..c720c299d --- /dev/null +++ b/templates/taxi/car_detail.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} + +{% block content %} +

{{ car.model }}

+

Manufacturer: {{ car.manufacturer.name }} ({{ car.manufacturer.country }})

+
+

Drivers:

+
    + {% for driver in drivers %} +
  • {{ driver.first_name }} {{ driver.last_name }} ({{ driver.license_number }})
  • + {% endfor %} + +
+
+{% endblock %} diff --git a/templates/taxi/car_list.html b/templates/taxi/car_list.html new file mode 100644 index 000000000..0cc091a9f --- /dev/null +++ b/templates/taxi/car_list.html @@ -0,0 +1,22 @@ +{% extends "base.html" %} + +{% block content %} +

Car list

+ + + + + + + + + {% for car in car_list %} + + + + + {% endfor %} + +
ModelManufacture
{{ car.model }}{{ car.manufacturer.name }}
+ +{% endblock %} diff --git a/templates/taxi/driver_detail.html b/templates/taxi/driver_detail.html new file mode 100644 index 000000000..465419814 --- /dev/null +++ b/templates/taxi/driver_detail.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} + +{% block content %} +

{{ driver.first_name }} {{ driver.last_name }}

+

({{ driver.license_number }})

+
+

Cars:

+
    + {% for car in cars %} +
  • {{ car.model }}
  • + {% endfor %} + +
+
+{% endblock %} diff --git a/templates/taxi/driver_list.html b/templates/taxi/driver_list.html new file mode 100644 index 000000000..6dfadc812 --- /dev/null +++ b/templates/taxi/driver_list.html @@ -0,0 +1,28 @@ +{% extends "base.html" %} + +{% block content %} +

Driver list

+ + + + + + + + + + + + {% for driver in driver_list %} + + + + + + + + {% endfor %} + +
UsernameFirst NameLast NameEmailLicense Number
{{ driver.username }}{{ driver.first_name }}{{ driver.last_name }}{{ driver.email }}{{ driver.license_number }}
+ +{% endblock %} diff --git a/templates/taxi/manufacturer_list.html b/templates/taxi/manufacturer_list.html new file mode 100644 index 000000000..9bd1bf350 --- /dev/null +++ b/templates/taxi/manufacturer_list.html @@ -0,0 +1,22 @@ +{% extends "base.html" %} + +{% block content %} +

Manufacturer list

+ + + + + + + + + {% for manufacturer in manufacturer_list %} + + + + + {% endfor %} + +
NameCountry
{{ manufacturer.name }}{{ manufacturer.country }}
+ +{% endblock %}