Мне нужно удалить элемент корзины при нажатии на кнопку удалить, но он не работает. Страница корзины просто refre sh и заканчивает свое выполнение. Мне нужно создать рабочую функцию корзины для удаления элементов из нее.
Это мой файл шаблона - views. html:
{% extends 'pro/base.html' %}
{% block content %}
{% if empty %}
<div class="container">
<h1 style="text-align:center">{{ empty_message }}</h1>
</div>
{% else %}
<div class="row">
<div class="col-sm-8 col-sm-offset-2">
<table class="table">
<thead>
<th>Item</th>
<th>Quantity</th>
<th>Price</th>
<th></th>
</thead>
<tfoot>
<th></th>
<th></th>
<th>Total:{{ cart.total }}</th>
<th></th>
</tfoot>
{% for item in cart.cartitem_set.all %}
<tr>
<td>{{ item.product }} {% if item.variations.all %}<ul>
{% for subitem in item.variations.all %}
<li>{{ subitem.category|capfirst }}:{{ subitem.title|capfirst }}</li>
{% endfor %}
</ul>{% endif %}</td>
<td>{{ item.quantity }}</td>
<td>{{ item.product.price }}</td>
<td><a href="{% url 'remove_from_cart' item.id %}">Remove</a></td>
</tr
{% endfor %}
</table>
</div>
</div>
{% endif %}
{% endblock %}
Это мои views.py:
from django.shortcuts import render
from .models import Cart,CartItem
from django.http import HttpResponseRedirect
from django.urls import reverse
from app.models import Product,Variation
def cart(request):
try:
the_id = request.session['cart_id']
except:
the_id = None
if the_id:
cart = Cart.objects.get(id=the_id)
context = {'cart':cart}
else:
empty_message = "you have notthing in your cart.Carry on your shoopping"
context = {'empty':True,"empty_message":empty_message}
template = 'cart/view.html'
return render(request,template,context)
def remove_from_cart(request,id):
try:
the_id = request.session['cart_id']
cart = Cart.objects.get(id=the_id)
except:
return HttpResponseRedirect(reverse("cart"))
cartitem = CartItem.objects.get(id=id)
cartitem.cart = None
cartitem.save()
return HttpResponseRedirect(reverse("cart"))
def add_to_cart(request,slug):
request.session.set_expiry(None)
try:
the_id = request.session['cart_id']
except:
new_cart = Cart()
new_cart.save()
request.session['cart_id'] = new_cart.id
the_id = new_cart.id
cart = Cart.objects.get(id=the_id)
try:
product = Product.objects.get(slug=slug)
except Product.DoesNotExist:
pass
except:
pass
product_var = []
if request.method == "POST":
qty = request.POST['qty']
for item in request.POST:
key = item
val = request.POST[key]
try:
v = Variation.objects.get(product=product,category__iexact=key,title__iexact=val)
product_var.append(v)
except:
pass
cart_item = CartItem.objects.create(cart=cart,product=product)
if len(product_var)>0:
cart_item.variations.add(*product_var)
cart_item.quantity = qty
cart_item.save()
new_total = 0
for item in cart.cartitem_set.all():
line_total = float(item.product.price)*item.quantity
new_total += line_total
request.session['item_count'] = cart.cartitem_set.count()
cart.total = new_total
cart.save()
return HttpResponseRedirect(reverse("cart"))
else:
return HttpResponseRedirect(reverse("cart"))
А это мой urls.py:
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path,include
from app import views
from cart.views import cart,add_to_cart,remove_from_cart
urlpatterns = [
path('',views.index,name='index'),
path('admin/', admin.site.urls),
path('s/',views.search,name='search'),
path('cart/',cart,name='cart'),
path('cart/<slug:slug>/',add_to_cart,name='add_to_cart'),
path('cart/<int:id>/',remove_from_cart,name='remove_from_cart'),
path('products/',views.all,name='products'),
path('produc/<slug:slug>/',views.single,name='single_product'),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)