Add the customer storefront on Django templates

Pages are rendered server-side (SEO, no build step), the interactive
parts talk to the existing REST API from the browser:

- home: banners, categories, hits and new arrivals
- catalog: filters (category, brand, price, stock), search, sorting,
  pagination, filter chips with one-click removal
- product card: gallery, price with discount, stock, characteristics,
  related products
- cart: localStorage, quantity controls, live totals
- checkout: contacts, delivery, promo code, bonuses, order submission
  with Idempotency-Key, success screen
- login: OTP by phone (debug code shown in dev) and phone/password
- account: profile, orders, addresses, bonuses through the API with JWT
- static pages from the admin

Supporting pieces:
- apps/storefront app with views, urls, templates and static CSS/JS
- money/discount/attribute template filters
- seed_demo management command with demo catalog, banners and pages
- Cyrillic search fallback for SQLite: local DB does not fold case for
  non-ASCII, production PostgreSQL does

Tests: 16 storefront cases (pages, filters, search, ordering, 404s,
filters formatting); the suite is 61 passing.
This commit is contained in:
apuc committed 2026-11-22 22:38:39 +03:00
1 parent b18e3ea703
commit 37d5d7b366
29 files changed
+2822 -7

No files matched your search

+151
View File
@@ -0,0 +1,151 @@
{% extends "storefront/base.html" %}
{% load storefront %}
{% block title %}{{ product.name }} — {{ site.site_name }}{% endblock %}
{% block description %}{{ product.short_description|default:product.name|truncatechars:160 }}{% endblock %}
{% block content %}
<div class="breadcrumbs">
<a href="{% url 'home' %}">Главная</a> /
<a href="{% url 'catalog' %}">Каталог</a> /
{% if product.category %}
<a href="{% url 'catalog' %}?category={{ product.category.slug }}">{{ product.category.name }}</a> /
{% endif %}
<span>{{ product.name }}</span>
</div>
<div class="product-layout">
<div>
<div class="gallery-main" data-gallery-main>
{% if images %}
<img src="{{ images.0.url }}" alt="{{ product.name }}" data-main-image>
{% else %}
<span class="placeholder">{{ product.name|slice:":1" }}</span>
{% endif %}
</div>
{% if images|length > 1 %}
<div class="gallery-thumbs">
{% for image in images %}
<button type="button" data-thumb="{{ image.url }}"
class="{% if forloop.first %}active{% endif %}"
aria-label="Фото {{ forloop.counter }}">
<img src="{{ image.url }}" alt="{{ image.alt|default:product.name }}" loading="lazy">
</button>
{% endfor %}
</div>
{% endif %}
</div>
<div class="product-info">
{% if product.brand %}<span class="product-brand">{{ product.brand }}</span>{% endif %}
<h1>{{ product.name }}</h1>
<div class="product-sku">Артикул: {{ product.sku }}</div>
<div class="product-price-lg">
<span class="price-now">{{ product.price|money }} ₽</span>
{% if product.old_price %}
<span class="price-old">{{ product.old_price|money }} ₽</span>
<span class="badge badge-new">−{{ product.price|discount_percent:product.old_price }}%</span>
{% endif %}
</div>
{% if product.stock > 0 %}
<div class="stock"><span class="dot"></span> В наличии: {{ product.stock }} шт.</div>
{% elif product.stock_status == 'preorder' %}
<div class="stock out"><span class="dot"></span> Под заказ</div>
{% else %}
<div class="stock out"><span class="dot"></span> Нет в наличии</div>
{% endif %}
{% if product.short_description %}
<p class="product-desc">{{ product.short_description }}</p>
{% endif %}
{% if product.stock > 0 %}
<div class="buy-row">
<div class="qty">
<button type="button" data-qty-minus aria-label="Меньше">−</button>
<input type="number" value="1" min="1" max="{{ product.stock }}"
data-qty-input aria-label="Количество">
<button type="button" data-qty-plus aria-label="Больше">+</button>
</div>
<button class="btn btn-accent btn-block"
data-add-to-cart
data-id="{{ product.id }}"
data-slug="{{ product.slug }}"
data-name="{{ product.name }}"
data-price="{{ product.price }}"
data-image="{{ product.main_image|default:'' }}"
data-stock="{{ product.stock }}"
data-qty-source="[data-qty-input]">
Добавить в корзину
</button>
</div>
<a class="btn btn-outline btn-block" href="{% url 'cart' %}">Перейти в корзину</a>
{% else %}
<button class="btn btn-outline btn-block" disabled>Нет в наличии</button>
{% endif %}
{% if product.description %}
<div class="product-desc">{{ product.description|linebreaks }}</div>
{% endif %}
</div>
</div>
{% if attributes %}
<section class="section" style="margin-top:48px">
<h2>Характеристики</h2>
<table class="specs">
{% for name, value in attributes.items %}
<tr><td>{{ name }}</td><td>{{ value|attr_value }}</td></tr>
{% endfor %}
</table>
</section>
{% endif %}
{% if related %}
<section class="section">
<div class="section-head">
<h2>Похожие товары</h2>
{% if product.category %}
<a href="{% url 'catalog' %}?category={{ product.category.slug }}">Вся категория</a>
{% endif %}
</div>
<div class="product-grid">
{% for item in related %}
{% include "storefront/_product_card.html" with product=item %}
{% endfor %}
</div>
</section>
{% endif %}
{% endblock %}
{% block scripts %}
<script>
document.querySelectorAll('[data-thumb]').forEach(function (button) {
button.addEventListener('click', function () {
var main = document.querySelector('[data-main-image]');
if (!main) { return; }
main.src = button.dataset.thumb;
document.querySelectorAll('[data-thumb]').forEach(function (other) {
other.classList.toggle('active', other === button);
});
});
});
(function () {
var input = document.querySelector('[data-qty-input]');
if (!input) { return; }
var minus = document.querySelector('[data-qty-minus]');
var plus = document.querySelector('[data-qty-plus]');
var max = parseInt(input.max || '999', 10);
function clamp(value) {
return Math.max(1, Math.min(max, value || 1));
}
minus.addEventListener('click', function () { input.value = clamp(parseInt(input.value, 10) - 1); });
plus.addEventListener('click', function () { input.value = clamp(parseInt(input.value, 10) + 1); });
input.addEventListener('change', function () { input.value = clamp(parseInt(input.value, 10)); });
})();
</script>
{% endblock %}