From 4ced056007183c395534bc68e550f8e7fb624806 Mon Sep 17 00:00:00 2001 From: Misha Goryn Date: Tue, 24 Sep 2024 19:47:12 +0300 Subject: [PATCH 1/2] Solution --- taxi/urls.py | 3 ++- taxi/views.py | 23 ++++++++++++++++++----- taxi_service/settings.py | 4 ++++ taxi_service/urls.py | 4 ++++ templates/includes/sidebar.html | 6 ++++++ templates/registration/login.html | 12 ++++++++++++ templates/registration/logout.html | 2 ++ templates/taxi/car_list.html | 2 +- templates/taxi/driver_list.html | 7 +++++-- templates/taxi/index.html | 1 + 10 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 templates/registration/login.html create mode 100644 templates/registration/logout.html diff --git a/taxi/urls.py b/taxi/urls.py index c663d6e2..5530d66e 100644 --- a/taxi/urls.py +++ b/taxi/urls.py @@ -1,4 +1,4 @@ -from django.urls import path +from django.urls import path, include from .views import ( index, @@ -22,6 +22,7 @@ path( "drivers//", DriverDetailView.as_view(), name="driver-detail" ), + path("taxi/", include("django.contrib.auth.urls")), ] app_name = "taxi" diff --git a/taxi/views.py b/taxi/views.py index 82ad312f..068bfe91 100644 --- a/taxi/views.py +++ b/taxi/views.py @@ -1,17 +1,24 @@ +from django.contrib.auth.decorators import login_required +from django.contrib.auth.mixins import LoginRequiredMixin +from django.http import HttpRequest 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) + 1 + request.session["num_visits"] = num_visits num_drivers = Driver.objects.count() num_cars = Car.objects.count() num_manufacturers = Manufacturer.objects.count() context = { + "num_visits": num_visits, "num_drivers": num_drivers, "num_cars": num_cars, "num_manufacturers": num_manufacturers, @@ -20,28 +27,34 @@ def index(request): 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") + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + current_user = self.request.user + context["current_user_id"] = current_user.id + return context diff --git a/taxi_service/settings.py b/taxi_service/settings.py index b6c0cf19..2b59b4e9 100644 --- a/taxi_service/settings.py +++ b/taxi_service/settings.py @@ -108,6 +108,10 @@ AUTH_USER_MODEL = "taxi.Driver" +LOGIN_URL = "login" +LOGIN_REDIRECT_URL = "taxi:index" +LOGOUT_REDIRECT_URL = "login" + # Internationalization # https://docs.djangoproject.com/en/4.0/topics/i18n/ diff --git a/taxi_service/urls.py b/taxi_service/urls.py index 8b94449f..26953fc6 100644 --- a/taxi_service/urls.py +++ b/taxi_service/urls.py @@ -13,6 +13,7 @@ 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path("blog/", include("blog.urls")) """ +from django.contrib.auth import views as auth_views from django.contrib import admin from django.urls import path, include from django.conf import settings @@ -20,6 +21,9 @@ urlpatterns = [ + path("taxi/", include("django.contrib.auth.urls")), path("admin/", admin.site.urls), + path("login/", auth_views.LoginView.as_view(), name="login"), + path("logout/", auth_views.LogoutView.as_view(), name="logout"), path("", include("taxi.urls", namespace="taxi")), ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) diff --git a/templates/includes/sidebar.html b/templates/includes/sidebar.html index b7cd72dc..351fe5d2 100644 --- a/templates/includes/sidebar.html +++ b/templates/includes/sidebar.html @@ -1,4 +1,10 @@