version backend3

This commit is contained in:
Flatlogic Bot 2026-03-13 19:32:45 +00:00
parent 7cf53e9789
commit fc0fcfb24f
7 changed files with 98 additions and 9 deletions

View File

@ -0,0 +1,44 @@
{% extends "base.html" %}
{% block title %}Shopping Cart | Ethio-gebeya{% endblock %}
{% block content %}
<main class="container py-4">
<h1 class="mb-4">Shopping Cart</h1>
{% if items %}
<table class="table">
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Price</th>
<th>Subtotal</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{% for item in items %}
<tr>
<td>{{ item.product.name }}</td>
<td>{{ item.quantity }}</td>
<td>{{ item.product.price }} ETB</td>
<td>{{ item.subtotal }} ETB</td>
<td><a href="{% url 'remove_from_cart' item.product.pk %}" class="btn btn-danger btn-sm">Remove</a></td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="mt-3">
<h4>Total: {{ total_price }} ETB</h4>
<a href="#" class="btn btn-primary">Proceed to Checkout</a>
</div>
{% else %}
<p>Your cart is empty.</p>
{% endif %}
<div class="mt-4">
<a href="{% url 'home' %}" class="btn btn-secondary">Continue Shopping</a>
</div>
</main>
{% endblock %}

View File

@ -15,6 +15,7 @@
<p class="card-text text-muted">{{ product.description|truncatewords:20 }}</p>
<p class="card-text fw-bold">Price: {{ product.price }} ETB</p>
<a href="{% url 'product_detail' product.pk %}" class="btn btn-primary">View Details</a>
<a href="{% url 'add_to_cart' product.pk %}" class="btn btn-success">Add to Cart</a>
</div>
</div>
</div>
@ -27,6 +28,7 @@
{% else %}
<a class="btn btn-primary" href="{% url 'login' %}">Sign In</a>
{% endif %}
<a class="btn btn-info" href="{% url 'cart_view' %}">View Cart</a>
</div>
</main>
{% endblock %}
{% endblock %}

View File

@ -11,13 +11,11 @@
<p class="h4 text-primary">Price: {{ product.price }} ETB</p>
<p>Stock: {{ product.stock }}</p>
<form method="post" action="#">
{% csrf_token %}
<button type="submit" class="btn btn-primary btn-lg">Add to Cart</button>
</form>
<a href="{% url 'add_to_cart' product.pk %}" class="btn btn-primary btn-lg">Add to Cart</a>
<a href="{% url 'home' %}" class="btn btn-link mt-3">Back to catalog</a>
<a href="{% url 'cart_view' %}" class="btn btn-info mt-3">View Cart</a>
</div>
</div>
</main>
{% endblock %}
{% endblock %}

View File

@ -1,7 +1,16 @@
from django.urls import path
from django.contrib.auth.views import LoginView, LogoutView
from .views import buyer_dashboard, dashboard_redirect, home, product_detail, seller_dashboard
from .views import (
add_to_cart,
buyer_dashboard,
cart_view,
dashboard_redirect,
home,
product_detail,
remove_from_cart,
seller_dashboard,
)
urlpatterns = [
path("", home, name="home"),
@ -11,4 +20,7 @@ urlpatterns = [
path("dashboard/", dashboard_redirect, name="dashboard"),
path("dashboard/buyer/", buyer_dashboard, name="buyer_dashboard"),
path("dashboard/seller/", seller_dashboard, name="seller_dashboard"),
]
path("cart/", cart_view, name="cart_view"),
path("cart/add/<int:pk>/", add_to_cart, name="add_to_cart"),
path("cart/remove/<int:pk>/", remove_from_cart, name="remove_from_cart"),
]

View File

@ -87,4 +87,37 @@ def seller_dashboard(request):
"total_revenue": sales_summary["total_revenue"] or 0,
"recent_sales": recent_sales,
}
return render(request, "core/seller_dashboard.html", context)
return render(request, "core/seller_dashboard.html", context)
def add_to_cart(request, pk):
product = get_object_or_404(Product, pk=pk)
cart = request.session.get("cart", {})
product_id = str(pk)
cart[product_id] = cart.get(product_id, 0) + 1
request.session["cart"] = cart
return redirect("home")
def remove_from_cart(request, pk):
cart = request.session.get("cart", {})
product_id = str(pk)
if product_id in cart:
del cart[product_id]
request.session["cart"] = cart
return redirect("cart_view")
def cart_view(request):
cart = request.session.get("cart", {})
items = []
total_price = 0
for product_id, quantity in cart.items():
try:
product = Product.objects.get(pk=product_id)
items.append({"product": product, "quantity": quantity, "subtotal": product.price * quantity})
total_price += product.price * quantity
except Product.DoesNotExist:
continue
context = {"items": items, "total_price": total_price}
return render(request, "core/cart.html", context)