From acfe69cf159cd9b5caa0a3d30ba970ae0a4d93ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BE=D0=BD=D1=8F=20C=D0=B0=D0=B2=D0=BA=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0?= Date: Wed, 20 Nov 2024 22:40:14 +0200 Subject: [PATCH 1/2] Solution --- taxi/urls.py | 15 ++++++++++++++- taxi/views.py | 21 ++++++++++++++++----- taxi_service/settings.py | 2 ++ taxi_service/urls.py | 1 + templates/includes/sidebar.html | 25 +++++++++++++++++++------ templates/registration/login.html | 13 +++++++++++++ templates/registration/logout.html | 6 ++++++ templates/taxi/driver_list.html | 4 +++- templates/taxi/index.html | 2 ++ 9 files changed, 76 insertions(+), 13 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..a67ccea7 100644 --- a/taxi/urls.py +++ b/taxi/urls.py @@ -1,4 +1,5 @@ from django.urls import path +from django.contrib.auth import views as auth_views from .views import ( index, @@ -20,7 +21,19 @@ path("cars//", CarDetailView.as_view(), name="car-detail"), path("drivers/", DriverListView.as_view(), name="driver-list"), path( - "drivers//", DriverDetailView.as_view(), name="driver-detail" + "drivers//", DriverDetailView.as_view(), name="driver-detail", + ), + path( + "login/", + auth_views.LoginView.as_view(template_name="registration/login.html"), + name="login" + ), + path( + "logout/", + auth_views.LogoutView.as_view( + template_name="registration/logout.html" + ), + name="logout" ), ] diff --git a/taxi/views.py b/taxi/views.py index 82ad312f..ad1434a5 100644 --- a/taxi/views.py +++ b/taxi/views.py @@ -1,11 +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) + 1 + request.session["num_visits"] = num_visits num_drivers = Driver.objects.count() num_cars = Car.objects.count() @@ -15,33 +20,39 @@ def index(request): "num_drivers": num_drivers, "num_cars": num_cars, "num_manufacturers": num_manufacturers, + "num_visits": request.session.get("num_visits"), } 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 + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context["current_user"] = self.request.user + return context -class DriverDetailView(generic.DetailView): + +class DriverDetailView(LoginRequiredMixin, generic.DetailView): model = Driver queryset = Driver.objects.prefetch_related("cars__manufacturer") diff --git a/taxi_service/settings.py b/taxi_service/settings.py index b6c0cf19..e8f0543a 100644 --- a/taxi_service/settings.py +++ b/taxi_service/settings.py @@ -133,3 +133,5 @@ # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" + +LOGIN_REDIRECT_URL = "/" diff --git a/taxi_service/urls.py b/taxi_service/urls.py index 8b94449f..baf83d1a 100644 --- a/taxi_service/urls.py +++ b/taxi_service/urls.py @@ -22,4 +22,5 @@ urlpatterns = [ path("admin/", admin.site.urls), path("", include("taxi.urls", namespace="taxi")), + path("accounts/", include("django.contrib.auth.urls")), ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) diff --git a/templates/includes/sidebar.html b/templates/includes/sidebar.html index b7cd72dc..8befe801 100644 --- a/templates/includes/sidebar.html +++ b/templates/includes/sidebar.html @@ -1,6 +1,19 @@ - + \ No newline at end of file diff --git a/templates/registration/login.html b/templates/registration/login.html new file mode 100644 index 00000000..b6aa00e4 --- /dev/null +++ b/templates/registration/login.html @@ -0,0 +1,13 @@ +{% extends "base.html" %} + +{% block content %} +

Login

+ {% if form.errors %} +

Invalid username or password

+ {% endif %} +
+ {% csrf_token %} + {{ form.as_p }} + +
+{% endblock %} diff --git a/templates/registration/logout.html b/templates/registration/logout.html new file mode 100644 index 00000000..d7041a9f --- /dev/null +++ b/templates/registration/logout.html @@ -0,0 +1,6 @@ +{% extends "base.html" %} + +{% block content %} +

Logged out

+

You have been logged out. Login again?

+{% endblock %} diff --git a/templates/taxi/driver_list.html b/templates/taxi/driver_list.html index c6c3e823..907a47a4 100644 --- a/templates/taxi/driver_list.html +++ b/templates/taxi/driver_list.html @@ -6,7 +6,9 @@

Driver list

    {% for driver in driver_list%}
  • - {{ driver.username }} + + {{ driver.username }} {% if driver == current_user %}(Me){% endif %} + ({{ driver.first_name }} {{ driver.last_name }})
  • {% endfor %} diff --git a/templates/taxi/index.html b/templates/taxi/index.html index 13c59aa8..cc37b1ef 100644 --- a/templates/taxi/index.html +++ b/templates/taxi/index.html @@ -3,6 +3,8 @@ {% block content %}

    Taxi Service Home

    Welcome to Best Taxi Ever!

    +

    You have visited this page {{ num_visits }} times.

    +

    Dynamic content

    The Taxi service has the following record counts:

      From 24529bbc6ceae1f53b2d75869cd0eec64c162945 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BE=D0=BD=D1=8F=20C=D0=B0=D0=B2=D0=BA=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0?= Date: Thu, 21 Nov 2024 09:00:05 +0200 Subject: [PATCH 2/2] changed according to comments --- templates/registration/logout.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/registration/logout.html b/templates/registration/logout.html index d7041a9f..975d1840 100644 --- a/templates/registration/logout.html +++ b/templates/registration/logout.html @@ -2,5 +2,5 @@ {% block content %}

      Logged out

      -

      You have been logged out. Login again?

      +

      You have been logged out. Login again?

      {% endblock %}