Is it better boss?
rodzic
8c550aa0dd
commit
1a45479cd0
|
@ -1,15 +1,64 @@
|
|||
<!-- basic interface to add produts, created for testing -->
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>My Store</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Welcome to My Store</h1>
|
||||
<form action="{% url 'add_to_cart' %}" method="POST">
|
||||
<input type="text" id="product_id" name="product_id">
|
||||
{% extends "base.html" %}
|
||||
{% block content %}
|
||||
<h1>Shopping Cart</h1>
|
||||
|
||||
<h2>Cart Items</h2>
|
||||
<ul id="cart-items"></ul>
|
||||
|
||||
<h2>Add Item to Cart</h2>
|
||||
<form id="add-to-cart-form" method="POST" action="/cart/item/">
|
||||
{% csrf_token %}
|
||||
<button type="submit">Add Item to Cart</button>
|
||||
<div>
|
||||
<label for="id_product_id">Product ID:</label>
|
||||
<input type="number" id="id_product_id" name="product_id" required>
|
||||
</div>
|
||||
<div>
|
||||
<label for="id_quantity">Quantity:</label>
|
||||
<input type="number" id="id_quantity" name="quantity" required>
|
||||
</div>
|
||||
<button type="submit">Add to Cart</button>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
||||
<script>
|
||||
// Fetch cart items and display them on page load
|
||||
$(document).ready(function() {
|
||||
fetchCartItems();
|
||||
});
|
||||
|
||||
// Add item to cart on form submission
|
||||
$('#add-to-cart-form').on('submit', function(event) {
|
||||
event.preventDefault();
|
||||
const form = $(this);
|
||||
const formData = new FormData(form[0]); // Serialize the form data correctly
|
||||
$.ajax({
|
||||
type: form.attr('method'),
|
||||
url: form.attr('action'),
|
||||
data: formData, // Use the serialized form data
|
||||
dataType: 'json',
|
||||
processData: false, // Prevent jQuery from processing the data
|
||||
contentType: false, // Let the browser set the content type
|
||||
success: function(data) {
|
||||
alert(data.message);
|
||||
fetchCartItems();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Fetch cart items and update the display
|
||||
function fetchCartItems() {
|
||||
fetch('/cart/')
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
const cartItems = data.cart_items;
|
||||
const cartItemsList = $('#cart-items');
|
||||
cartItemsList.empty();
|
||||
cartItems.forEach(item => {
|
||||
const li = $('<li>').text(`Product ID: ${item.product_id}, Quantity: ${item.quantity}`);
|
||||
cartItemsList.append(li);
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
</script>
|
||||
{% endblock %}
|
|
@ -9,17 +9,19 @@ from wagtail import urls as wagtail_urls
|
|||
from wagtail.documents import urls as wagtaildocs_urls
|
||||
|
||||
from search import views as search_views
|
||||
from cart import views as cart_views
|
||||
from store.views import CartItemView, CartView
|
||||
|
||||
urlpatterns = [
|
||||
path("django-admin/", admin.site.urls),
|
||||
path("admin/", include(wagtailadmin_urls)),
|
||||
path("documents/", include(wagtaildocs_urls)),
|
||||
path("search/", search_views.search, name="search"),
|
||||
path('cart/items/', cart_views.view_cart, name='cart_items'),
|
||||
path('add_to_cart/', cart_views.add_to_cart, name='add_to_cart'),
|
||||
path('remove_from_cart/', cart_views.remove_from_cart, name='remove_from_cart'),
|
||||
path('cart/', CartView.as_view(), name='cart'),
|
||||
path('cart/item/', CartItemView.as_view(), name='add_to_cart'),
|
||||
path('cart/item/<int:cart_item_id>/', CartItemView.as_view(),
|
||||
name='cart_item_remove'),
|
||||
path('', TemplateView.as_view(template_name='index.html'), name='index'),
|
||||
|
||||
]
|
||||
|
||||
|
||||
|
@ -29,7 +31,8 @@ if settings.DEBUG:
|
|||
|
||||
# Serve static and media files from development server
|
||||
urlpatterns += staticfiles_urlpatterns()
|
||||
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||
urlpatterns += static(settings.MEDIA_URL,
|
||||
document_root=settings.MEDIA_ROOT)
|
||||
|
||||
urlpatterns = urlpatterns + [
|
||||
# For anything not caught by a more specific rule above, hand over to
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
from django.shortcuts import render, redirect
|
||||
|
||||
|
||||
def add_to_cart(request):
|
||||
if request.method == 'POST':
|
||||
product_id = request.POST["product_id"]
|
||||
cart = request.session['cart']
|
||||
if product_id in cart:
|
||||
cart[product_id] += 1
|
||||
else:
|
||||
cart[product_id] = 1
|
||||
|
||||
# Save the session
|
||||
request.session.modified = True
|
||||
return redirect('../cart/items')
|
||||
|
||||
|
||||
def remove_from_cart(request):
|
||||
if request.method == 'POST':
|
||||
product_id = request.POST["product_id"]
|
||||
cart = request.session.get('cart', {})
|
||||
if product_id in cart:
|
||||
del cart[product_id]
|
||||
request.session['cart'] = cart
|
||||
request.session.modified = True
|
||||
|
||||
# Redirect to the cart page or product listing page
|
||||
return redirect('../cart/items')
|
||||
|
||||
|
||||
def view_cart(request):
|
||||
cart = request.session.get('cart', {})
|
||||
products = "good"
|
||||
return render(request, 'store/cart_items.html',
|
||||
{'cart': cart, 'products': products})
|
|
@ -1,50 +0,0 @@
|
|||
<!-- cart.html -->
|
||||
<!-- content will be changed after consultation -->
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Shopping Cart</title>
|
||||
<style>
|
||||
table {
|
||||
border-collapse: collapse;
|
||||
width: 100%;
|
||||
}
|
||||
th, td {
|
||||
padding: 8px;
|
||||
text-align: left;
|
||||
border-bottom: 1px solid #ddd;
|
||||
}
|
||||
th {
|
||||
background-color: #f2f2f2;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
{% if cart %}
|
||||
<table>
|
||||
<tr>
|
||||
<th>Product</th>
|
||||
<th>Quantity</th>
|
||||
<th>Price</th>
|
||||
<th>Total</th>
|
||||
<th>Remove</th>
|
||||
</tr>
|
||||
{% for product_id, quantity in cart.items %}
|
||||
<tr>
|
||||
<td>{{ product_id }}</td>
|
||||
<td>{{ quantity }}</td>
|
||||
<td>{{ products_dict }}</td>
|
||||
<td></td>
|
||||
<td><form action="{% url 'remove_from_cart' %}" method="POST">
|
||||
<input type="hidden" value= {{ product_id }} name= "product_id" >
|
||||
{% csrf_token %}
|
||||
<button type="submit">Remove</button></a></form></td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
<p class="cart-total">Cart Total: {{ cart_total }}</p>
|
||||
{% else %}
|
||||
<p class="empty-cart-msg">Your cart is empty.</p>
|
||||
{% endif %}
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,39 @@
|
|||
from django.http import JsonResponse
|
||||
from django.views import View
|
||||
|
||||
|
||||
class CartView(View):
|
||||
def get(self, request):
|
||||
cart = request.session.get('cart', {})
|
||||
cart_items = cart.items()
|
||||
response_data = {
|
||||
'cart_items': [{
|
||||
'product_id': key,
|
||||
'quantity': value
|
||||
} for key, value in cart_items]
|
||||
}
|
||||
return JsonResponse(response_data)
|
||||
|
||||
|
||||
class CartItemView(View):
|
||||
allowed_methods = ['POST', 'DELETE']
|
||||
|
||||
def post(self, request):
|
||||
try:
|
||||
product_id = request.POST.get('product_id')
|
||||
quantity = request.POST.get('quantity')
|
||||
cart = request.session.get('cart', {})
|
||||
cart[product_id] = quantity
|
||||
request.session['cart'] = cart
|
||||
|
||||
return JsonResponse({'message': 'Item added to cart.'})
|
||||
except Exception as e:
|
||||
return JsonResponse({'error': str(e)}, status=400)
|
||||
|
||||
def delete(self, request, cart_item_id):
|
||||
cart = request.session.get('cart', {})
|
||||
if cart_item_id in cart:
|
||||
del cart[cart_item_id]
|
||||
request.session['cart'] = cart
|
||||
|
||||
return JsonResponse({'message': 'Item removed from cart.'})
|
Ładowanie…
Reference in New Issue