From 4ef9e6c6075c31f8227579090807cd20bf111608 Mon Sep 17 00:00:00 2001 From: Vlad Rymarchuk Date: Tue, 1 Oct 2024 16:01:59 +0300 Subject: [PATCH 1/3] Make migrations, load the data, implemented a visit counter on the home screen --- taxi/urls.py | 2 +- taxi/views.py | 5 +++++ templates/taxi/index.html | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/taxi/urls.py b/taxi/urls.py index c663d6e2..28cb8941 100644 --- a/taxi/urls.py +++ b/taxi/urls.py @@ -1,6 +1,6 @@ from django.urls import path -from .views import ( +from taxi.views import ( index, CarListView, CarDetailView, diff --git a/taxi/views.py b/taxi/views.py index 82ad312f..f626ba54 100644 --- a/taxi/views.py +++ b/taxi/views.py @@ -7,6 +7,10 @@ def index(request): """View function for the home page of the site.""" + num_visits = request.session.get("num_visits", 0) + num_visits += 1 + request.session["num_visits"] = num_visits + num_drivers = Driver.objects.count() num_cars = Car.objects.count() num_manufacturers = Manufacturer.objects.count() @@ -15,6 +19,7 @@ def index(request): "num_drivers": num_drivers, "num_cars": num_cars, "num_manufacturers": num_manufacturers, + "num_visits": num_visits, } return render(request, "taxi/index.html", context=context) diff --git a/templates/taxi/index.html b/templates/taxi/index.html index 13c59aa8..ac9e7c56 100644 --- a/templates/taxi/index.html +++ b/templates/taxi/index.html @@ -10,4 +10,7 @@

Dynamic content

  • Drivers: {{ num_drivers }}
  • Manufacturers: {{ num_manufacturers }}
  • +

    + You have visited this page {{ num_visits }} time{{ num_visits|pluralize }}. +

    {% endblock %} From 49ceea3cbc566638f3470b8d7558c58880571022 Mon Sep 17 00:00:00 2001 From: Vlad Rymarchuk Date: Tue, 1 Oct 2024 16:24:27 +0300 Subject: [PATCH 2/3] created registarion/login.html, logout.html --- taxi/views.py | 13 ++++++++----- taxi_service/settings.py | 2 ++ taxi_service/urls.py | 1 + templates/registration/login.html | 15 +++++++++++++++ templates/registration/logout.html | 7 +++++++ 5 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 templates/registration/login.html create mode 100644 templates/registration/logout.html diff --git a/taxi/views.py b/taxi/views.py index f626ba54..5707b1b6 100644 --- a/taxi/views.py +++ b/taxi/views.py @@ -1,9 +1,12 @@ +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.""" @@ -25,28 +28,28 @@ 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") diff --git a/taxi_service/settings.py b/taxi_service/settings.py index b6c0cf19..b09ff418 100644 --- a/taxi_service/settings.py +++ b/taxi_service/settings.py @@ -132,4 +132,6 @@ # Default primary key field type # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field +LOGIN_REDIRECT_URL = "//" + DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" diff --git a/taxi_service/urls.py b/taxi_service/urls.py index 8b94449f..5d781485 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/registration/login.html b/templates/registration/login.html new file mode 100644 index 00000000..3af54fad --- /dev/null +++ b/templates/registration/login.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} + +{% block content %} +

    Login

    + {% if form.errors %} +

    Invalid credentials

    + {% 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..4d11189e --- /dev/null +++ b/templates/registration/logout.html @@ -0,0 +1,7 @@ +{% extends "base.html" %} +{% block content %} +

    Logged out

    + Click here to log in again +
    + Click here to return to the catalog page +{% endblock %} From 2baacb2e6ef436d2453bdfda82e9646f38bd477a Mon Sep 17 00:00:00 2001 From: Vlad Rymarchuk Date: Tue, 1 Oct 2024 16:42:10 +0300 Subject: [PATCH 3/3] result --- taxi_service/settings.py | 2 +- templates/includes/sidebar.html | 16 ++++++++++++++++ .../{logout.html => logged_out.html} | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-) rename templates/registration/{logout.html => logged_out.html} (62%) diff --git a/taxi_service/settings.py b/taxi_service/settings.py index b09ff418..1b7d4885 100644 --- a/taxi_service/settings.py +++ b/taxi_service/settings.py @@ -132,6 +132,6 @@ # Default primary key field type # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field -LOGIN_REDIRECT_URL = "//" +LOGIN_REDIRECT_URL = "/" DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" diff --git a/templates/includes/sidebar.html b/templates/includes/sidebar.html index b7cd72dc..f62bd602 100644 --- a/templates/includes/sidebar.html +++ b/templates/includes/sidebar.html @@ -1,4 +1,20 @@