From 1737407c5645b2712abf0b94816b796ef9824f82 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 20 Nov 2024 16:29:01 +0100 Subject: [PATCH 1/3] solution --- static/css/styles.css | 9 +++++++++ taxi/views.py | 17 +++++++++++------ taxi_service/settings.py | 4 ++++ taxi_service/urls.py | 2 ++ templates/base.html | 9 ++++++++- templates/includes/sidebar.html | 4 ++++ templates/includes/sidebar_login.html | 12 ++++++++++++ templates/registration/logged_out.html | 6 ++++++ templates/registration/login.html | 15 +++++++++++++++ templates/taxi/driver_list.html | 3 +++ templates/taxi/index.html | 7 ++++++- 11 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 templates/includes/sidebar_login.html create mode 100644 templates/registration/logged_out.html create mode 100644 templates/registration/login.html diff --git a/static/css/styles.css b/static/css/styles.css index 1d8a1b3d..e0341a2b 100644 --- a/static/css/styles.css +++ b/static/css/styles.css @@ -1,3 +1,12 @@ body { margin-top: 20px; } +.custom-btn { + text-decoration: none; + color: inherit; +} + +.custom-btn:hover { + color: white; + background-color: #0d6efd; +} \ No newline at end of file diff --git a/taxi/views.py b/taxi/views.py index 82ad312f..0094e8c9 100644 --- a/taxi/views.py +++ b/taxi/views.py @@ -1,47 +1,52 @@ +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_drivers = Driver.objects.count() num_cars = Car.objects.count() num_manufacturers = Manufacturer.objects.count() + num_visits = request.session.get("num_visits", 1) + request.session["num_visits"] = num_visits + 1 context = { "num_drivers": num_drivers, "num_cars": num_cars, "num_manufacturers": num_manufacturers, + "num_visits": 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 -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..0ddbea33 100644 --- a/taxi_service/settings.py +++ b/taxi_service/settings.py @@ -108,6 +108,10 @@ AUTH_USER_MODEL = "taxi.Driver" +LOGIN_REDIRECT_URL = "/cars/" + +LOGOUT_REDIRECT_URL = "/" + # Internationalization # https://docs.djangoproject.com/en/4.0/topics/i18n/ diff --git a/taxi_service/urls.py b/taxi_service/urls.py index 8b94449f..f29151fb 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 import admin from django.urls import path, include from django.conf import settings @@ -22,4 +23,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/base.html b/templates/base.html index f4a0b6cf..17c2dda3 100644 --- a/templates/base.html +++ b/templates/base.html @@ -24,7 +24,7 @@ {% endblock %} -
+
{% block content %}{% endblock %} @@ -33,6 +33,13 @@ {% endblock %}
+
+ + {% block login %} + {% include "includes/sidebar_login.html" %} + {% endblock %} + +
diff --git a/templates/includes/sidebar.html b/templates/includes/sidebar.html index b7cd72dc..5b2a9567 100644 --- a/templates/includes/sidebar.html +++ b/templates/includes/sidebar.html @@ -1,4 +1,8 @@ diff --git a/templates/taxi/index.html b/templates/taxi/index.html index 13c59aa8..7ffdc5e2 100644 --- a/templates/taxi/index.html +++ b/templates/taxi/index.html @@ -3,11 +3,16 @@ {% block content %}

Taxi Service Home

Welcome to Best Taxi Ever!

-

Dynamic content

+ {% if user.is_authenticated %} +

Welcome {{ user.username }}!

+ {% else %} +

For see more information you must log in!

+ {% endif %}

The Taxi service has the following record counts:

+

You visited us {{ num_visits }} time{{ num_visits|pluralize }}.

{% endblock %} From 169495122a87c3c8d12337e821a692407b687fb0 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 20 Nov 2024 16:31:04 +0100 Subject: [PATCH 2/3] solution --- taxi/views.py | 1 + 1 file changed, 1 insertion(+) diff --git a/taxi/views.py b/taxi/views.py index 0094e8c9..7d7fdb1c 100644 --- a/taxi/views.py +++ b/taxi/views.py @@ -5,6 +5,7 @@ from .models import Driver, Car, Manufacturer + @login_required def index(request): """View function for the home page of the site.""" From 7cd274a9d9c099a24c0bcedefc933cd74eb37497 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 21 Nov 2024 08:37:38 +0100 Subject: [PATCH 3/3] fix error --- templates/registration/login.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/registration/login.html b/templates/registration/login.html index 6b4fe5f9..e5c73442 100644 --- a/templates/registration/login.html +++ b/templates/registration/login.html @@ -3,7 +3,7 @@ {% block content %}
Login in your account
- {% if error %} + {% if form.errors %}

{{ form.errors }}Login or password is wrong

{% endif %}