-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
168 additions
and
6 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class CoreConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "core" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from django.core.paginator import InvalidPage | ||
from django.core.paginator import Paginator | ||
from django.db.models import QuerySet | ||
from django.http import Http404 | ||
from django.http import HttpRequest | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
|
||
def paginate_queryset(request: HttpRequest, queryset: QuerySet, page_size: int = 20): | ||
""" | ||
Paginates a queryset, and returns a page object. | ||
copied from https://github.com/carltongibson/neapolitan/blob/main/src/neapolitan/views.py | ||
""" | ||
paginator = Paginator(queryset, page_size) | ||
page_number = request.GET.get("page") or 1 | ||
try: | ||
page_number = int(page_number) | ||
except ValueError: | ||
if page_number == "last": | ||
page_number = paginator.num_pages | ||
else: | ||
msg = "Page is not 'last', nor can it be converted to an int." | ||
raise Http404(_(msg)) | ||
|
||
try: | ||
return paginator.page(page_number) | ||
except InvalidPage as exc: | ||
msg = "Invalid page (%s): %s" | ||
raise Http404(_(msg) % (page_number, str(exc))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from django.forms import ModelForm | ||
|
||
from .models import Product | ||
|
||
|
||
class ProductForm(ModelForm): | ||
class Meta: | ||
model = Product | ||
fields = ("id", "name", "description", "price", "slug", "sku") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from django.urls import path | ||
|
||
from . import views | ||
|
||
app_name = "products" | ||
|
||
urlpatterns = [ | ||
path("products/", views.product_list, name="product_list"), | ||
path("products/create/", views.product_create, name="product_create"), | ||
path("products/<int:pk>/", views.product_detail, name="product_detail"), | ||
path("products/<int:pk>/update/", views.product_update, name="product_update"), | ||
path("products/<int:pk>/delete/", views.product_delete, name="product_delete"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from core.utils import paginate_queryset | ||
from django.http import HttpRequest | ||
from django.http import HttpResponse | ||
from django.shortcuts import get_object_or_404 | ||
from django.shortcuts import redirect | ||
from django.template.response import TemplateResponse | ||
from django.views.decorators.http import require_http_methods | ||
|
||
from .forms import ProductForm | ||
from .models import Product | ||
|
||
|
||
def product_list(request: HttpRequest): | ||
products = Product.objects.all() | ||
return TemplateResponse( | ||
request, | ||
"products/product_list.html", | ||
context={"products": paginate_queryset(request, products)}, | ||
) | ||
|
||
|
||
def product_detail(request: HttpRequest, pk: int): | ||
product = get_object_or_404(Product.objects, pk=pk) | ||
return TemplateResponse( | ||
request, | ||
"products/product_detail.html", | ||
context={"product": product}, | ||
) | ||
|
||
|
||
def product_create(request: HttpRequest): | ||
form = ProductForm(request.POST or None) | ||
if request.method == "POST" and form.is_valid(): | ||
form.save() | ||
return redirect("products:product_list") | ||
return TemplateResponse( | ||
request, | ||
"products/product_create.html", | ||
context={"form": form}, | ||
) | ||
|
||
|
||
def product_update(request: HttpRequest, pk: int): | ||
product = get_object_or_404(Product.objects, pk=pk) | ||
form = ProductForm(request.POST or None, instance=product) | ||
if request.method == "POST" and form.is_valid(): | ||
form.save() | ||
return redirect("products:product_detail", pk=pk) | ||
return TemplateResponse( | ||
request, | ||
"products/product_update.html", | ||
context={"product": product, "form": form}, | ||
) | ||
|
||
|
||
@require_http_methods(["DELETE"]) | ||
def product_delete(request: HttpRequest, pk: int): | ||
Product.objects.filter(pk=pk).delete() | ||
return HttpResponse("OK") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>My Website</title> | ||
<script src="https://cdn.tailwindcss.com"></script> | ||
</head> | ||
<body> | ||
<main class="container mx-auto prose"> | ||
{% block content %} | ||
{% endblock %} | ||
</main> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{% extends 'base.html' %} | ||
|
||
{% block content %} | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Product Name</th> | ||
<th>Price</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{% for product in products %} | ||
<tr> | ||
<td>{{ product.name }}</td> | ||
<td>{{ product.price }}</td> | ||
</tr> | ||
{% endfor %} | ||
</tbody> | ||
</table> | ||
|
||
<div class="pagination"> | ||
{% if products.has_previous %} | ||
<a href="?page={{ products.previous_page_number }}">Previous</a> | ||
{% endif %} | ||
<span class="current-page">{{ products.number }}</span> | ||
{% if products.has_next %} | ||
<a href="?page={{ products.next_page_number }}">Next</a> | ||
{% endif %} | ||
</div> | ||
{% endblock %} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters