Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution #863

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions taxi/migrations/0002_alter_manufacturer_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.1 on 2024-11-06 13:01

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('taxi', '0001_initial'),
]

operations = [
migrations.AlterModelOptions(
name='manufacturer',
options={'ordering': ['name']},
),
]
3 changes: 3 additions & 0 deletions taxi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ class Manufacturer(models.Model):
name = models.CharField(max_length=255, unique=True)
country = models.CharField(max_length=255)

class Meta:
ordering = ["name"]


class Driver(AbstractUser):
license_number = models.CharField(max_length=255, unique=True)
Expand Down
22 changes: 21 additions & 1 deletion taxi/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
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"
27 changes: 27 additions & 0 deletions taxi/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.shortcuts import render
from django.views import generic

from taxi.models import Driver, Car, Manufacturer

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

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


class ManufacturerListView(generic.ListView):
model = Manufacturer
queryset = Manufacturer.objects.all()
paginate_by = 5


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


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")
1 change: 1 addition & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
{% block content %}{% endblock %}

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

</div>
Expand Down
20 changes: 20 additions & 0 deletions templates/includes/pagination.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% if is_paginated %}
<ul class="pagination">
{% if page_obj.has_previous %}
<li class="page-item">
<a href="?page={{ page_obj.previous_page_number }}" class="page-link">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 href="?page={{ page_obj.next_page_number }}" class="page-link">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>
17 changes: 17 additions & 0 deletions templates/taxi/car_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends "base.html" %}

{% block content %}
<strong>Manufacturer:</strong>
<ul>
<li>Name: {{ car.manufacturer.name }}</li>
<li>Country: {{ car.manufacturer.country }}</li>
</ul>
<strong>Drivers</strong>
<ul>
{% for driver in car.drivers.all %}
<li>{{ driver.username }}</li>
{% empty %}
<p>Car doesn't have any drivers</p>
{% endfor %}
</ul>
{% 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>
<ul>
{% for car in car_list%}
<li><a href="{% url 'taxi:car-detail' car.id %}">{{ car.model }}</a></li>
{% empty %}
<p>There are no cars</p>
{% endfor %}
</ul>
{% 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 %}
<h1>{{ driver.username }} cars:</h1>
<ul>
{% for car in driver.cars.all %}
<li>{{ car.model }}</li>
{% empty %}
<p>Driver doesn't have any cars</p>
{% endfor %}
</ul>
{% 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>
<ul>
{% for driver in driver_list %}
<li><a href="{% url 'taxi:driver-detail' driver.id %}">{{ driver.first_name }}</a></li>
{% empty %}
<p>There are no drivers</p>
{% endfor %}
</ul>
{% 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>Manufacturers</h1>
<ul>
{% for manufacturer in manufacturer_list%}
<li>{{ manufacturer.name }}: {{ manufacturer.country }}</li>
{% empty %}
<p>There are no manufacturers</p>
{% endfor %}
</ul>
{% endblock %}
Loading