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

py-taxi-service-class-based-views #391

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 13 additions & 1 deletion taxi/urls.py
Original file line number Diff line number Diff line change
@@ -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/<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"
41 changes: 41 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 .models import Driver, Car, Manufacturer

Expand All @@ -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
7 changes: 4 additions & 3 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2"
crossorigin="anonymous">
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2"
crossorigin="anonymous">
<!-- Add additional CSS in static file -->
{% load static %}
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
Expand All @@ -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>
15 changes: 15 additions & 0 deletions templates/taxi/car_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends "base.html" %}

{% block content %}
<h1>{{ car.model }}</h1>
<p>Manufacturer: {{ car.manufacturer.name }} ({{ car.manufacturer.country }})</p>
<div>
<h3>Drivers:</h3>
<ul>
{% for driver in drivers %}
<li>{{ driver.first_name }} {{ driver.last_name }} ({{ driver.license_number }})</li>
{% endfor %}

</ul>
</div>
{% endblock %}
22 changes: 22 additions & 0 deletions templates/taxi/car_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% extends "base.html" %}

{% block content %}
<h1>Car list</h1>
<table class="table">
<thead>
<tr>
<th>Model</th>
<th>Manufacture</th>
</tr>
</thead>
<tbody>
{% for car in car_list %}
<tr>
<td><a href="{% url 'taxi:car-detail' car.id %}">{{ car.model }}</a></td>
<td>{{ car.manufacturer.name }}</td>
</tr>
{% endfor %}
</tbody>
</table>

{% endblock %}
15 changes: 15 additions & 0 deletions templates/taxi/driver_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends "base.html" %}

{% block content %}
<h1>{{ driver.first_name }} {{ driver.last_name }}</h1>
<p>({{ driver.license_number }})</p>
<div>
<h3>Cars:</h3>
<ul>
{% for car in cars %}
<li>{{ car.model }}</li>
{% endfor %}

</ul>
</div>
{% endblock %}
28 changes: 28 additions & 0 deletions templates/taxi/driver_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{% extends "base.html" %}

{% block content %}
<h1>Driver list</h1>
<table class="table">
<thead>
<tr>
<th>Username</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>License Number</th>
</tr>
</thead>
<tbody>
{% for driver in driver_list %}
<tr>
<td><a href="{% url 'taxi:driver-detail' driver.id %}">{{ driver.username }}</a></td>
<td>{{ driver.first_name }}</td>
<td>{{ driver.last_name }}</td>
<td>{{ driver.email }}</td>
<td>{{ driver.license_number }}</td>
</tr>
{% endfor %}
</tbody>
</table>

{% endblock %}
22 changes: 22 additions & 0 deletions templates/taxi/manufacturer_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% extends "base.html" %}

{% block content %}
<h1>Manufacturer list</h1>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Country</th>
</tr>
</thead>
<tbody>
{% for manufacturer in manufacturer_list %}
<tr>
<td>{{ manufacturer.name }}</td>
<td>{{ manufacturer.country }}</td>
</tr>
{% endfor %}
</tbody>
</table>

{% endblock %}
Loading