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 #875

Open
wants to merge 3 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
9 changes: 9 additions & 0 deletions static/css/styles.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
body {
margin-top: 20px;
}
.custom-btn {
text-decoration: none;
color: inherit;
}

.custom-btn:hover {
color: white;
background-color: #0d6efd;
}
16 changes: 11 additions & 5 deletions taxi/views.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,53 @@
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)

Choose a reason for hiding this comment

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

The session visit count should initialize to 0 instead of 1 if it doesn't exist. This ensures the count accurately reflects the number of visits. Consider changing request.session.get("num_visits", 1) to request.session.get("num_visits", 0).

request.session["num_visits"] = num_visits + 1
Comment on lines +16 to +17

Choose a reason for hiding this comment

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

The session visit count logic should initialize num_visits to 0 instead of 1 if it doesn't exist. This is because the current logic will always start the count at 1 on the first visit, which might not be the intended behavior. Consider changing request.session.get("num_visits", 1) to request.session.get("num_visits", 0).


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")
4 changes: 4 additions & 0 deletions taxi_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/

Expand Down
2 changes: 2 additions & 0 deletions taxi_service/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
9 changes: 8 additions & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
{% endblock %}

</div>
<div class="col-sm-10 ">
<div class="col-sm-8 custom-centered p-5">

{% block content %}{% endblock %}

Expand All @@ -33,6 +33,13 @@
{% endblock %}

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

{% block login %}
{% include "includes/sidebar_login.html" %}
{% endblock %}

</div>
</div>
</div>
</body>
Expand Down
4 changes: 4 additions & 0 deletions templates/includes/sidebar.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<ul class="sidebar-nav">
{% if user.is_authenticated %}
<a href="{% url "taxi:driver-detail" pk=user.id %}">{{ user.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 driver-detail URL pattern expects a pk parameter that corresponds to the user.id. If user.id does not match the expected parameter for the driver-detail view, this could lead to a NoReverseMatch error. Verify that the pk is the correct parameter for this URL.

Choose a reason for hiding this comment

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

Ensure that the driver-detail URL pattern correctly expects a pk parameter that matches user.id. Mismatches could lead to NoReverseMatch errors. Verify that the URL pattern is correctly defined in your urls.py.

{% 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/includes/sidebar_login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class="text-end">

{% if user.is_authenticated %}
<button type="button" class="btn btn-outline-primary">
<a href="{% url 'logout' %}" class="custom-btn">Logout</a>
Comment on lines +4 to +5

Choose a reason for hiding this comment

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

The <a> tag should not be nested inside a <button> element as it is not valid HTML and can cause unexpected behavior. Consider using only the <a> tag styled as a button, or use JavaScript to handle the button click event to redirect the user.

</button>
Comment on lines +4 to +6

Choose a reason for hiding this comment

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

Avoid nesting <a> tags inside <button> elements, as this is not valid HTML. Consider using an <a> tag styled as a button or handle the click event with JavaScript.

{% else %}
<button type="button" class="btn btn-outline-primary me-2">
<a href="{% url 'login' %}" class="custom-btn">Login</a>
Comment on lines +8 to +9

Choose a reason for hiding this comment

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

Similar to the logout button, avoid nesting the <a> tag inside a <button> element. Use the <a> tag with button styling or handle the click event with JavaScript.

</button>
Comment on lines +8 to +10

Choose a reason for hiding this comment

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

Avoid nesting <a> tags inside <button> elements, as this is not valid HTML. Consider using an <a> tag styled as a button or handle the click event with JavaScript.

{% endif %}
</div>
6 changes: 6 additions & 0 deletions templates/registration/logged_out.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends "base.html" %}

{% block content %}
<p>Logged out!</p>
<a href="{% url 'login' %}">Login again!</a>
{% endblock %}
15 changes: 15 additions & 0 deletions templates/registration/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends "base.html" %}

{% block content %}
<div>
<h5>Login in your account</h5>
{% if form.errors %}
<p style="color: red">{{ form.errors }}Login or password is wrong</p>
Comment on lines +6 to +7

Choose a reason for hiding this comment

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

The error message concatenates form.errors with a static message, which might not be clear. Consider displaying form.errors separately or providing a more user-friendly message to improve clarity.

{% endif %}
<form action="{% url 'login' %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Login">
</form>
</div>
{% endblock %}
3 changes: 3 additions & 0 deletions templates/taxi/driver_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ <h1>Driver list</h1>
<li>
<a href="{% url "taxi:driver-detail" pk=driver.id %}">{{ driver.username }}</a>
({{ driver.first_name }} {{ driver.last_name }})
{% if user.id == driver.id%}
<strong>(Me)</strong>
{% endif %}
</li>
{% endfor %}
</ul>
Expand Down
7 changes: 6 additions & 1 deletion templates/taxi/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
{% block content %}
<h1>Taxi Service Home</h1>
<p>Welcome to Best Taxi Ever!</p>
<h2>Dynamic content</h2>
{% if user.is_authenticated %}
<h2>Welcome <a href="{% url "taxi:driver-detail" pk=user.id %}">{{ user.username }}</a>!</h2>

Choose a reason for hiding this comment

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

Ensure that the driver-detail URL pattern correctly expects a pk parameter that matches user.id. Mismatches could lead to NoReverseMatch errors. Verify that the URL pattern is correctly defined in your urls.py.

{% else %}
<h2>For see more information you must <a href="{% url 'login' %}">log in</a>!</h2>
{% endif %}
<p>The Taxi service has the following record counts:</p>
<ul>
<li><strong>Cars:</strong> {{ num_cars }}</li>
<li><strong>Drivers:</strong> {{ num_drivers }}</li>
<li><strong>Manufacturers:</strong> {{ num_manufacturers }}</li>
</ul>
<p>You visited us {{ num_visits }} time{{ num_visits|pluralize }}.</p>
{% endblock %}
Loading