-
Notifications
You must be signed in to change notification settings - Fork 845
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
base: master
Are you sure you want to change the base?
solution #875
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} |
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) | ||
request.session["num_visits"] = num_visits + 1 | ||
Comment on lines
+16
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The session visit count logic should initialize |
||
|
||
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") |
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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the |
||
{% 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> | ||
|
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
</button> | ||
Comment on lines
+4
to
+6
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid nesting |
||
{% 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the logout button, avoid nesting the |
||
</button> | ||
Comment on lines
+8
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid nesting |
||
{% endif %} | ||
</div> |
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 %} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error message concatenates |
||
{% endif %} | ||
<form action="{% url 'login' %}" method="post"> | ||
{% csrf_token %} | ||
{{ form.as_p }} | ||
<input type="submit" value="Login"> | ||
</form> | ||
</div> | ||
{% endblock %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the |
||
{% 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 %} |
There was a problem hiding this comment.
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)
torequest.session.get("num_visits", 0)
.