Answer the question
In order to leave comments, you need to log in
Why doesn't Django add to cart work?
Error when adding product to cart:
Page not found (404)
Request Method: GET
Request URL: 127.0.0.1:8000/cart
Raised by: shop.views.product_list
No Category matches the given query.
I transfer from one application to the second.
Shop app:
urls.py
from django.urls import path, include
from . import views
app_name='shop'
urlpatterns=[
path('',views.product_list, name='product_list'),
path('menu/',views.menu, name='menu'),
path('delivery/',views.delivery, name='delivery'),
path('news/',views.news, name='news'),
path('news/<int:id>/<slug:slug>/',views.news_detail, name='news_detail'),
path('<str:category_slug>/',views.product_list, name='product_list_by_category'),
path('<int:id>/<slug:slug>/',views.product_detail,name='product_detail'),
]
from django.shortcuts import render, HttpResponse, get_object_or_404
from .models import Category, Product, Articles
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
# Create your views here.
def index(request):
return render(request, 'shop/product/index.html')
def product_list(request,category_slug=None):
category = None
categories= Category.objects.all()
products = Product.objects.filter(available=True)
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
products = products.filter(category=category)
paginator = Paginator(products, 1)
page = request.GET.get('page')
try:
posts = paginator.page(page)
except PageNotAnInteger:
posts = paginator.page(1)
except EmptyPage:
posts = paginator.page(paginator.num_pages)
return render(request,'shop/product/index.html',{'category': category,'categories': categories,'products': products, 'page': page,'posts': posts})
def menu(request):
categories= Category.objects.all()
return render(request,'shop/product/menu.html',{'categories': categories})
def delivery(request):
categories= Category.objects.all()
return render(request,'shop/product/delivery.html',{'categories': categories})
def news(request):
categories= Category.objects.all()
articles= Articles.objects.all()
return render(request,'shop/product/news.html',{'articles': articles,'categories': categories})
def news_detail(request,id,slug):
categories= Category.objects.all()
news = get_object_or_404(Articles,id=id,slug=slug)
return render(request, 'shop/product/news_detail.html', {'article': news,'categories': categories})
def product_detail(request,id,slug):
categories= Category.objects.all()
product = get_object_or_404(Product,id=id,slug=slug,available=True)
return render(request, 'shop/product/detail.html', {'product': product,'categories': categories})
<form action="{% url 'cart:cart_add' product.id %}" method="post">
{{ cart_product_form }}
{% csrf_token %}
<button class="main-content_block-item_btn" type="submit">
В корзину
</button>
</form>
from django.urls import path, include
from . import views
app_name='cart'
urlpatterns = [
path('add/<int:product_id>/', views.cart_add, name='cart_add'),
path('remove/<int:product_id>/', views.cart_remove, name='cart_remove'),
path('', views.cart_detail, name='cart_detail'),
]
from django.shortcuts import render, redirect, get_object_or_404
from django.views.decorators.http import require_POST
from shop.models import Product
from .cart import Cart
from .forms import CartAddProductForm
@require_POST
def cart_add(request, product_id):
cart = Cart(request)
product = get_object_or_404(Product, id=product_id)
form = CartAddProductForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
cart.add(product=product,
quantity=cd['quantity'],
update_quantity=cd['update'])
return redirect('cart:cart_detail')
def cart_remove(request, product_id):
cart = Cart(request)
product = get_object_or_404(Product, id=product_id)
cart.remove(product)
return redirect('cart:cart_detail')
def cart_detail(request):
cart = Cart(request)
return render(request, 'cart/detail.html', {'cart': cart})
from decimal import Decimal
from django.conf import settings
from shop.models import Product
class Cart(object):
def __init__(self, request):
"""
Инициализируем корзину
"""
self.session = request.session
cart = self.session.get(settings.CART_SESSION_ID)
if not cart:
# save an empty cart in the session
cart = self.session[settings.CART_SESSION_ID] = {}
self.cart = cart
def add(self, product, quantity=1, update_quantity=False):
"""
Добавить продукт в корзину или обновить его количество.
"""
product_id = str(product.id)
if product_id not in self.cart:
self.cart[product_id] = {'quantity': 0,
'price': str(product.price)}
if update_quantity:
self.cart[product_id]['quantity'] = quantity
else:
self.cart[product_id]['quantity'] += quantity
self.save()
def save(self):
# Обновление сессии cart
self.session[settings.CART_SESSION_ID] = self.cart
# Отметить сеанс как "измененный", чтобы убедиться, что он сохранен
self.session.modified = True
def remove(self, product):
"""
Удаление товара из корзины.
"""
product_id = str(product.id)
if product_id in self.cart:
del self.cart[product_id]
self.save()
def __iter__(self):
"""
Перебор элементов в корзине и получение продуктов из базы данных.
"""
product_ids = self.cart.keys()
# получение объектов product и добавление их в корзину
products = Product.objects.filter(id__in=product_ids)
for product in products:
self.cart[str(product.id)]['product'] = product
for item in self.cart.values():
item['price'] = Decimal(item['price'])
item['total_price'] = item['price'] * item['quantity']
yield item
def __len__(self):
"""
Подсчет всех товаров в корзине.
"""
return sum(item['quantity'] for item in self.cart.values())
def get_total_price(self):
"""
Подсчет стоимости товаров в корзине.
"""
return sum(Decimal(item['price']) * item['quantity'] for item in
self.cart.values())
def clear(self):
# удаление корзины из сессии
del self.session[settings.CART_SESSION_ID]
self.session.modified = True
from django import forms
PRODUCT_QUANTITY_CHOICES = [(i, str(i)) for i in range(1, 21)]
class CartAddProductForm(forms.Form):
quantity = forms.TypedChoiceField(choices=PRODUCT_QUANTITY_CHOICES, coerce=int)
update = forms.BooleanField(required=False, initial=False, widget=forms.HiddenInput)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question