Skip to content

Commit

Permalink
Class based forms
Browse files Browse the repository at this point in the history
  • Loading branch information
valerii-kashpur committed Jan 26, 2025
1 parent a33c2b2 commit 172c5f6
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 40 deletions.
12 changes: 11 additions & 1 deletion taxi/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
CarDetailView,
DriverListView,
DriverDetailView,
ManufacturerListView,
ManufacturerListView, CarCreateView, CarUpdateView, CarDeleteView,
ManufacturerCreateView, ManufacturerUpdateView, ManufacturerDeleteView,
)

urlpatterns = [
Expand All @@ -16,8 +17,17 @@
ManufacturerListView.as_view(),
name="manufacturer-list",
),
path("manufacturers/create", ManufacturerCreateView.as_view(),
name="manufacturer-create"),
path("manufacturers/<int:pk>/update", ManufacturerUpdateView.as_view(),
name="manufacturer-update"),
path("manufacturers/<int:pk>/delete", ManufacturerDeleteView.as_view(),
name="manufacturer-delete"),
path("cars/", CarListView.as_view(), name="car-list"),
path("cars/<int:pk>/", CarDetailView.as_view(), name="car-detail"),
path("cars/create/", CarCreateView.as_view(), name="car-create"),
path("cars/<int:pk>/update/", CarUpdateView.as_view(), name="car-update"),
path("cars/<int:pk>/delete/", CarDeleteView.as_view(), name="car-delete"),
path("drivers/", DriverListView.as_view(), name="driver-list"),
path(
"drivers/<int:pk>/", DriverDetailView.as_view(), name="driver-detail"
Expand Down
44 changes: 44 additions & 0 deletions taxi/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from django.urls import reverse_lazy
from django.views import generic
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import CreateView, UpdateView, DeleteView

from .models import Driver, Car, Manufacturer

Expand Down Expand Up @@ -52,3 +54,45 @@ class DriverListView(LoginRequiredMixin, generic.ListView):
class DriverDetailView(LoginRequiredMixin, generic.DetailView):
model = Driver
queryset = Driver.objects.all().prefetch_related("cars__manufacturer")


class CarCreateView(LoginRequiredMixin, CreateView):
model = Car
fields = "__all__"
template_name = "taxi/car_form.html"
success_url = reverse_lazy("taxi:car-list")


class CarUpdateView(LoginRequiredMixin, UpdateView):
model = Car
fields = "__all__"
template_name = "taxi/car_form.html"
success_url = reverse_lazy("taxi:car-list")


class CarDeleteView(LoginRequiredMixin, DeleteView):
model = Car
fields = "__all__"
template_name = "taxi/car_confirm_delete_form.html"
success_url = reverse_lazy("taxi:car-list")


class ManufacturerCreateView(LoginRequiredMixin, CreateView):
model = Manufacturer
fields = "__all__"
template_name = "taxi/manufacturer_form.html"
success_url = reverse_lazy("taxi:manufacturer-list")


class ManufacturerUpdateView(LoginRequiredMixin, UpdateView):
model = Manufacturer
fields = "__all__"
template_name = "taxi/manufacturer_form.html"
success_url = reverse_lazy("taxi:manufacturer-list")


class ManufacturerDeleteView(LoginRequiredMixin, DeleteView):
model = Manufacturer
fields = "__all__"
template_name = "taxi/manufacturer_confirm_delete.html"
success_url = reverse_lazy("taxi:manufacturer-list")
18 changes: 9 additions & 9 deletions taxi_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# Build paths inside the project like this: BASE_DIR / "subdir".
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

Expand Down Expand Up @@ -43,6 +42,8 @@
"django.contrib.messages",
"django.contrib.staticfiles",
"debug_toolbar",
"crispy_forms",
"crispy_bootstrap4",
"taxi",
]

Expand Down Expand Up @@ -77,7 +78,6 @@

WSGI_APPLICATION = "taxi_service.wsgi.application"


# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

Expand All @@ -88,26 +88,25 @@
}
}


# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation."
"UserAttributeSimilarityValidator",
"UserAttributeSimilarityValidator",
},
{
"NAME": "django.contrib.auth.password_validation."
"MinimumLengthValidator",
"MinimumLengthValidator",
},
{
"NAME": "django.contrib.auth.password_validation."
"CommonPasswordValidator",
"CommonPasswordValidator",
},
{
"NAME": "django.contrib.auth.password_validation."
"NumericPasswordValidator",
"NumericPasswordValidator",
},
]

Expand All @@ -126,7 +125,6 @@

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

Expand All @@ -139,4 +137,6 @@
# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap4"

CRISPY_TEMPLATE_PACK = "bootstrap4"
9 changes: 9 additions & 0 deletions templates/taxi/car_confirm_delete_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends "base.html" %}
{% block content %}
<h3>Delete car</h3>
<p>Are you sure you want to delete "{{ car.name }}"?</p>
<form method="post">
{% csrf_token %}
<input type="submit" value="Yes, delete" class="btn btn-primary">
</form>
{% endblock %}
11 changes: 7 additions & 4 deletions templates/taxi/car_detail.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
{% extends "base.html" %}

{% block content %}
<h2>Car Details: <a class="btn btn-primary" href={% url 'taxi:car-update' pk=car.id %}>Update</a> <a
class="btn btn-danger"
href={% url 'taxi:car-delete' pk=car.id %}>Delete</a></h2>
<p>Manufacturer: ({{ car.manufacturer.name }}, {{ car.manufacturer.country }})</p>
<h1>Drivers</h1>
<h5>Drivers</h5>
<hr>
<ul>
{% for driver in car.drivers.all %}
<li>{{ driver.username }} ({{ driver.first_name }} {{ driver.last_name }})</li>
{% endfor %}
{% for driver in car.drivers.all %}
<li>{{ driver.username }} ({{ driver.first_name }} {{ driver.last_name }})</li>
{% endfor %}
</ul>
{% endblock %}
10 changes: 10 additions & 0 deletions templates/taxi/car_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "base.html" %}
{% load crispy_forms_filters %}
{% block content %}
<h3>{{ object|yesno:"Update,Create" }} New Car</h3>
<form method="post" novalidate>
{% csrf_token %}
{{ form|crispy }}
<input class="btn btn-primary" type="submit" value="Submit">
</form>
{% endblock %}
3 changes: 2 additions & 1 deletion templates/taxi/car_list.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{% extends "base.html" %}

{% block content %}
<h1>Car list</h1>
<h1>Car list <a href={% url 'taxi:car-create' %}>+</a>
</h1>
{% if car_list %}
<ul>
{% for car in car_list %}
Expand Down
9 changes: 9 additions & 0 deletions templates/taxi/manufacturer_confirm_delete.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends "base.html" %}
{% block content %}
<h3>Delete Manufacturer</h3>
<p>Are you sure you want to delete "{{ manufacturer.name }}"?</p>
<form method="post">
{% csrf_token %}
<input type="submit" value="Yes, delete" class="btn btn-primary">
</form>
{% endblock %}
10 changes: 10 additions & 0 deletions templates/taxi/manufacturer_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "base.html" %}
{% load crispy_forms_filters %}
{% block content %}
<h3>{{ object|yesno:"Update, Create" }} manufacturer</h3>
<form method="post">
{% csrf_token %}
{{ form|crispy }}
<input type="submit" value="Submit">
</form>
{% endblock %}
61 changes: 36 additions & 25 deletions templates/taxi/manufacturer_list.html
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
{% extends "base.html" %}

{% block content %}
<h1>Manufacturer List
</h1>
<h1>Manufacturer List <a href={% url 'taxi:manufacturer-create' %}>+</a>
</h1>

{% if manufacturer_list %}
<table class="table">
{% if manufacturer_list %}
<table class="table">
<tr>
<th>ID</th>
<th>Name</th>
<th>Country</th>
<th>Update</th>
<th>Delete</th>
</tr>

{% for manufacturer in manufacturer_list %}
<tr>
<th>ID</th>
<th>Name</th>
<th>Country</th>
<td>
{{ manufacturer.id }}
</td>
<td>
{{ manufacturer.name }}
</td>
<td>
{{ manufacturer.country }}
</td>
<td>
<a class="btn btn-primary"
href={% url 'taxi:manufacturer-update' pk=manufacturer.id %}>Update</a>
</td>
<td>
<a
class="btn btn-danger"
href={% url 'taxi:manufacturer-delete' pk=manufacturer.id %}>Delete</a>
</td>
</tr>
{% endfor %}
</table>

{% for manufacturer in manufacturer_list %}
<tr>
<td>
{{ manufacturer.id }}
</td>
<td>
{{ manufacturer.name }}
</td>
<td>
{{ manufacturer.country }}
</td>
</tr>
{% endfor %}
</table>

{% else %}
<p>There are no manufacturers in the service.</p>
{% endif %}
{% else %}
<p>There are no manufacturers in the service.</p>
{% endif %}
{% endblock %}

0 comments on commit 172c5f6

Please sign in to comment.