Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazar Serdyuk committed Oct 30, 2024
1 parent 15a2836 commit c60cc64
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 6 deletions.
16 changes: 15 additions & 1 deletion taxi/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
from django.urls import path

from .views import index
from .views import (index, ManufacturerListView,
CarListView,
CarDetailView,
DriverListView,
DriverDetailView)

urlpatterns = [
path("", index, name="index"),
path("manufacturers/", ManufacturerListView.as_view(),
name="manufacturer-list"),
path("cars/", CarListView.as_view(),
name="car-list"),
path("cars/<int:pk>/", CarDetailView.as_view(),
name="car-detail"),
path("drivers/", DriverListView.as_view(),
name="driver-list"),
path("drivers/<int:pk>/", DriverDetailView.as_view(),
name="driver-detail"),
]

app_name = "taxi"
30 changes: 29 additions & 1 deletion taxi/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.shortcuts import render

from django.views import generic
from taxi.models import Driver, Car, Manufacturer


Expand All @@ -13,3 +13,31 @@ def index(request):
}

return render(request, "taxi/index.html", context=context)


class ManufacturerListView(generic.ListView):
model = Manufacturer
template_name = "taxi/manufacturer_list.html"
queryset = Manufacturer.objects.order_by("name")
paginate_by = 5


class CarListView(generic.ListView):
model = Car
paginate_by = 5
queryset = (Car.objects.select_related("manufacturer").
prefetch_related("drivers"))


class CarDetailView(generic.DetailView):
model = Car


class DriverListView(generic.ListView):
model = Driver
paginate_by = 5


class DriverDetailView(generic.DetailView):
model = Driver
queryset = Driver.objects.prefetch_related("cars__manufacturer")
2 changes: 2 additions & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
{% block content %}{% endblock %}

{% block pagination %}
{% include "includes/pagination.html" %}
{% endblock %}


</div>
</div>
</div>
Expand Down
11 changes: 11 additions & 0 deletions templates/includes/pagination.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% if is_paginated %}
<ul class="pagination">
{% if page_obj.has_previous %}
<li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}">Prev</a></li>
{% endif %}
<li class="page-item active"><span class="page-link">{{ page_obj.number }} of {{ paginator.num_pages }}</span></li>
{% if page_obj.has_next %}
<li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">Next</a></li>
{% endif %}
</ul>
{% endif %}
8 changes: 4 additions & 4 deletions templates/includes/sidebar.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<ul class="sidebar-nav">
<li><a href="#">Home page</a></li>
<li><a href="#">Manufacturers</a></li>
<li><a href="#">Cars</a></li>
<li><a href="#">Drivers</a></li>
<li><a href="{% url 'taxi:index' %}">Home page</a></li>
<li><a href="{% url 'taxi:manufacturer-list' %}">Manufacturers</a></li>
<li><a href="{% url 'taxi:car-list' %}">Cars</a></li>
<li><a href="{% url 'taxi:driver-list' %}">Drivers</a></li>
</ul>
18 changes: 18 additions & 0 deletions templates/taxi/car_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends "base.html" %}

{% block content %}
<h1>Car Details</h1>
<h3>{{ car.model }}</h3>
<p>Manufacturer: {{ car.manufacturer.name }}</p>
<p>Country: {{ car.manufacturer.country }}</p>
{% if car.drivers.all %}
<p>Drivers:</p>
<ul>
{% for driver in car.drivers.all %}
<li><a href="{% url 'taxi:driver-detail' driver.id %}">{{ driver.username }}</a></li>
{% endfor %}
</ul>
{% else %}
<h2>No drivers</h2>
{% endif %}
{% endblock %}
12 changes: 12 additions & 0 deletions templates/taxi/car_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends "base.html" %}

{% block content %}
<h1>Car List</h1>
{% if car_list %}
<ul>
{% for car in car_list %}
<li><a href="{% url 'taxi:car-detail' car.id %}">{{ car.model }}</a></li>
{% endfor %}
</ul>
{% endif %}
{% endblock %}
14 changes: 14 additions & 0 deletions templates/taxi/driver_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends "base.html" %}

{% block content %}
{% if driver.cars.all %}
<h3>Cars:</h3>
<ul>
{% for car in driver.cars.all %}
<li><a href="{% url 'taxi:car-detail' car.id %}">{{ car.model }}</a></li>
{% endfor %}
</ul>
{% else %}
<h2>No cars</h2>
{% endif %}
{% endblock %}
12 changes: 12 additions & 0 deletions templates/taxi/driver_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends "base.html" %}

{% block content %}
<h1>Driver List</h1>
{% if driver_list %}
<ul>
{% for driver in driver_list %}
<li><a href="{% url 'taxi:driver-detail' driver.id %}">{{ driver.username }}</a></li>
{% endfor %}
</ul>
{% endif %}
{% endblock %}
12 changes: 12 additions & 0 deletions templates/taxi/manufacturer_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends "base.html" %}

{% block content %}
<h1>Manufacturer List</h1>
{% if manufacturer_list %}
<ul>
{% for manufacturer in manufacturer_list %}
<li>{{ manufacturer.name }}</li>
{% endfor %}
</ul>
{% endif %}
{% endblock %}

0 comments on commit c60cc64

Please sign in to comment.