Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Solution' #896

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions taxi/urls.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
from django.urls import path

from .views import (
index,
CarListView,
CarDetailView,
DriverListView,
DriverDetailView,
ManufacturerListView,
)
index, CarListView, CarDetailView,
DriverListView, DriverDetailView, ManufacturerListView)

urlpatterns = [
path("", index, name="index"),
path(
"manufacturers/",
ManufacturerListView.as_view(),
name="manufacturer-list",
),
path("manufacturers/",
ManufacturerListView.as_view(),
name="manufacturer-list"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the task requirements, URL names should use hyphens (-) instead of underscores (_). Please change manufacturer-list to manufacturer-list to follow this convention.

),
path("cars/", CarListView.as_view(), name="car-list"),
path("cars/<int:pk>/", CarDetailView.as_view(), name="car-detail"),

path("drivers/", DriverListView.as_view(), name="driver-list"),
path(
"drivers/<int:pk>/", DriverDetailView.as_view(), name="driver-detail"
),
path("drivers/<int:pk>/",
DriverDetailView.as_view(),
name="driver-detail"
),
]

app_name = "taxi"
22 changes: 17 additions & 5 deletions taxi/views.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,59 @@
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 django.contrib.auth import logout as auth_logout

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", 0)
request.session["num_visits"] = num_visits + 1

context = {
"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")


def logout(request):
auth_logout(request)
return render(request, "registration/logout.html")
omnigun marked this conversation as resolved.
Show resolved Hide resolved
Comment on lines +57 to +59

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logout function should redirect users to a different page after logging out, rather than rendering a template. Consider using HttpResponseRedirect or redirect to send users to a more appropriate page, such as the login page or home page.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ок

2 changes: 2 additions & 0 deletions taxi_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "/"
6 changes: 4 additions & 2 deletions taxi_service/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
2. Add a URL to urlpatterns: path("blog/", include("blog.urls"))
"""
from django.contrib import admin
from django.urls import path, include
from django.urls import path, include, re_path
from django.conf import settings
from django.conf.urls.static import static

from taxi.views import logout

urlpatterns = [
path("admin/", admin.site.urls),
path("", include("taxi.urls", namespace="taxi")),
path("accounts/", include("django.contrib.auth.urls")),
re_path(r"^logout/$", logout, name="logout")
omnigun marked this conversation as resolved.
Show resolved Hide resolved

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of re_path is unnecessary here since the logout URL pattern does not involve regular expressions. Consider using path instead for simplicity and consistency with other URL patterns.

] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
15 changes: 8 additions & 7 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,33 @@
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2"
crossorigin="anonymous">
<!-- Add additional CSS in static file -->

{% load static %}
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
</head>

<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-2">
<div class="container-fluid">
<div class="row">
<div class="col-sm-2">

{% block sidebar %}
{% include "includes/sidebar.html" %}
{% endblock %}

</div>
<div class="col-sm-10 ">
</div>
<div class="col-sm-10 ">

{% block content %}{% endblock %}

{% block pagination %}
{% include "includes/pagination.html" %}
<p>You have visited this page {{ num_visits }} time{{ num_visits|pluralize }}</p>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pluralize filter should be applied directly to the variable without spaces. Change {{ num_visits|pluralize }} to {{ num_visits|pluralize }} to ensure correct pluralization.

{% endblock %}

</div>
</div>
</div>
</div>
</body>

</html>
1 change: 1 addition & 0 deletions templates/includes/pagination.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
{% endif %}
</ul>
{% endif %}

7 changes: 7 additions & 0 deletions templates/includes/sidebar.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<ul class="sidebar-nav">
{% if user.is_authenticated %}
<li> User: <a href="{% url "taxi:driver-detail" pk=user.id %}"> {{ user.username }}</a></li>
omnigun marked this conversation as resolved.
Show resolved Hide resolved

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the URL name taxi:driver-detail follows the naming convention of using hyphens instead of underscores if applicable. Verify that this matches the URL pattern defined in urls.py.

<li><a href="{% url "logout" %}">Logout</a></li>
{% else %}
<li><a href="{% url "login" %}">Login</a></li>
{% endif %}
<br>
<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
12 changes: 12 additions & 0 deletions templates/registration/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends "base.html" %}
{% block content %}
<h1>Login</h1>
{% if form.errors %}
<p style="color: red">Invalid credentials</p>
{% endif %}
<form method="post" action="{% url 'login' %}">
omnigun marked this conversation as resolved.
Show resolved Hide resolved

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verify that the URL name 'login' matches the URL pattern defined in your project's urls.py. Ensure consistency with the naming conventions used throughout the project.

{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
{% endblock %}
5 changes: 5 additions & 0 deletions templates/registration/logout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% extends "base.html" %}
{% block content %}
<p>Logged out!</p>
<a href="{% url 'login'%}">Click here to login again.</a>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verify that the URL name 'login' matches the URL pattern defined in your project's urls.py. Ensure consistency with the naming conventions used throughout the project.

{% endblock %}
5 changes: 4 additions & 1 deletion templates/taxi/driver_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
<h1>Driver list</h1>
{% if driver_list %}
<ul>
{% for driver in driver_list%}
{% for driver in driver_list %}
<li>
<a href="{% url "taxi:driver-detail" pk=driver.id %}">{{ driver.username }}</a>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the URL name taxi:driver-detail follows the naming convention of using hyphens instead of underscores if applicable. Verify that this matches the URL pattern defined in urls.py.

({{ driver.first_name }} {{ driver.last_name }})
{% if driver.id == user.id %}
(Me)
{% endif %}
</li>
{% endfor %}
</ul>
Expand Down
Loading