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 #855

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,25 @@

from .views import index

from .views import (
ManufacturerListView, CarListView,
car_detail_view, 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>/", car_detail_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"
41 changes: 41 additions & 0 deletions taxi/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.http import HttpRequest, HttpResponse
from django.shortcuts import render
from django.views import generic

from taxi.models import Driver, Car, Manufacturer

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

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


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

def get_queryset(self):
return Manufacturer.objects.all().order_by("name")


class CarListView(generic.ListView):
model = Car
context_object_name = "car_list"
paginate_by = 5

def get_queryset(self):
return Car.objects.select_related("manufacturer")


def car_detail_view(request: HttpRequest, pk: int) -> HttpResponse:
car = Car.objects.get(pk=pk)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using get_object_or_404 instead of Car.objects.get(pk=pk) to handle cases where the car with the given primary key does not exist. This will return a 404 error page instead of raising an exception.

context = {
"car": car,
}
return render(request, "taxi/car_detail.html", context=context)


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


class DriverDetailView(generic.DetailView):
model = Driver
template_name = "taxi/driver_detail.html"
context_object_name = "driver"

def get_queryset(self):
return Driver.objects.prefetch_related("cars__manufacturer").all()
7 changes: 1 addition & 6 deletions templates/base.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<!DOCTYPE html>
<html lang="en">

<head>
{% block title %}<title>Taxi Service</title>{% endblock %}
<meta charset="utf-8">
Expand All @@ -13,27 +12,23 @@
{% load static %}
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
</head>

<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-2">

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

</div>
<div class="col-sm-10 ">

{% block content %}{% endblock %}

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

</div>
</div>
</div>
</body>

</html>
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>
Loading