Я пытаюсь создать сайт интернет-магазина django s =. при нажатии Добавить в корзину товары не отображаются в корзине.
my views.py:
from django.shortcuts import render, redirect, HttpResponseRedirect
from products.models import Product
from .models import Cart
from django.contrib import messages
from django.urls import reverse
def cart(request):
cart = Cart.objects.all()[0]
context = {"cart":cart}
template = 'shopping_cart/cart.html'
return render(request, template, context)
def add_to_cart(request):
cart = Cart.objects.all()[0]
if not Product in cart.Products.all():
cart.products.add(Product)
else:
cart.products.remove(Product)
return HttpResponseRedirect(reverse('cart'))
my models.py:
from __future__ import unicode_literals
from django.db import models
from products.models import Product
class Cart(models.Model):
products = models.ManyToManyField(Product, null=True, blank=True)
total = models.DecimalField(max_digits=100, decimal_places=2, default = 0.00)
def __unicode__(self):
return "Cart"
my urls.py:
from django.contrib import admin
from django.urls import path, include
from products import views
from shopping_cart import views as sc_views
urlpatterns = [
path('admin/', admin.site.urls),
path('cart/', sc_views.cart, name='cart'),
path('cart/', sc_views.add_to_cart, name='add_to_cart'),
path('', include('products.urls'))
]
models моих продуктов.py:
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=255)
price = models.FloatField()
stock = models.IntegerField()
image_url = models.CharField(max_length=2083)