Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
eLQeR committed Feb 12, 2024
1 parent 46d9023 commit ced8e27
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 12 deletions.
8 changes: 8 additions & 0 deletions static/css/styles.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
body {
margin-top: 20px;
}

.container-center{
border: 1px black solid;
margin: 200px;
background-color: azure;
text-align: center;
justify-content: center;
}
17 changes: 11 additions & 6 deletions taxi/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import render
from django.views import generic

from .models import Driver, Car, Manufacturer


@login_required
def index(request):
"""View function for the home page of the site."""

num_visits = request.session.get("num_visits", 0)
request.session["num_visits"] = num_visits + 1
num_drivers = Driver.objects.count()
num_cars = Car.objects.count()
num_manufacturers = Manufacturer.objects.count()
Expand All @@ -15,33 +19,34 @@ def index(request):
"num_drivers": num_drivers,
"num_cars": num_cars,
"num_manufacturers": num_manufacturers,
"num_visits": num_visits + 1
}

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


class ManufacturerListView(generic.ListView):
class ManufacturerListView(LoginRequiredMixin, generic.ListView):
model = Manufacturer
context_object_name = "manufacturer_list"
template_name = "taxi/manufacturer_list.html"
paginate_by = 5


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


class CarDetailView(generic.DetailView):
class CarDetailView(LoginRequiredMixin, generic.DetailView):
model = Car


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


class DriverDetailView(generic.DetailView):
class DriverDetailView(LoginRequiredMixin, generic.DetailView):
model = Driver
queryset = Driver.objects.prefetch_related("cars__manufacturer")
7 changes: 7 additions & 0 deletions taxi_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"django.contrib.messages",
"django.contrib.staticfiles",
"taxi",
"debug_toolbar",
]

MIDDLEWARE = [
Expand All @@ -50,6 +51,7 @@
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"debug_toolbar.middleware.DebugToolbarMiddleware",
]

ROOT_URLCONF = "taxi_service.urls"
Expand Down Expand Up @@ -119,6 +121,7 @@

USE_TZ = True

LOGIN_REDIRECT_URL = "/"

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/
Expand All @@ -133,3 +136,7 @@
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

INTERNAL_IPS = [
"127.0.0.1",
]
13 changes: 8 additions & 5 deletions taxi_service/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
from django.conf import settings
from django.conf.urls.static import static


urlpatterns = [
path("admin/", admin.site.urls),
path("", include("taxi.urls", namespace="taxi")),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns = [path("admin/", admin.site.urls),
path("", include("taxi.urls", namespace="taxi")),
path("__debug__/", include("debug_toolbar.urls")),
path("accounts/", include("django.contrib.auth.urls")),
] + static(
settings.STATIC_URL,
document_root=settings.STATIC_ROOT
)
8 changes: 8 additions & 0 deletions templates/includes/sidebar.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<ul class="sidebar-nav">
{% if user.is_authenticated %}
<p>User: {{ user.username }}</p>
<li><a href="{% url "logout" %}">Logout</a></li>

{% else %}
<li><a href="{% url "login" %}">Login</a></li>
<br>
{% endif %}
<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>
Expand Down
5 changes: 5 additions & 0 deletions templates/registration/logged_out.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% extends "base.html" %}
{% load static %}
{% block content %}
<h2>Ти вийшов з аккаунту!!!</h2>
{% endblock %}
19 changes: 19 additions & 0 deletions templates/registration/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% extends "base.html" %}
{% load static %}
{% block content %}
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<br>
<label>Login:
<br>
<input type="text" name="username">
</label>
<br>
<label>Password:
<br>
<input type="password" name="password">
</label>
<br>
<button type="submit">Submit</button>
</form>
{% endblock %}
2 changes: 1 addition & 1 deletion templates/taxi/driver_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ <h1>Driver list</h1>
{% for driver in driver_list%}
<li>
<a href="{% url "taxi:driver-detail" pk=driver.id %}">{{ driver.username }}</a>
({{ driver.first_name }} {{ driver.last_name }})
({{ driver.first_name }} {{ driver.last_name }} {% if request.user.id == driver.id %} (Me) {% endif %})
</li>
{% endfor %}
</ul>
Expand Down
1 change: 1 addition & 0 deletions templates/taxi/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ <h2>Dynamic content</h2>
<li><strong>Cars:</strong> {{ num_cars }}</li>
<li><strong>Drivers:</strong> {{ num_drivers }}</li>
<li><strong>Manufacturers:</strong> {{ num_manufacturers }}</li>
<li><strong>Num of your visits:</strong> {{ num_visits }}</li>
</ul>
{% endblock %}

0 comments on commit ced8e27

Please sign in to comment.