I am building a supermarket store, using Django. I have to give user the possibility to add/remove items to/from the Favourite list. So far i have done the following models.py
class Product(models.Model):
category = models.ForeignKey('Category', null=True, blank=True,
on_delete=models.SET_NULL)
sku = models.CharField(max_length=254, null=True, blank=True)
name = models.CharField(max_length=254)
description = models.TextField()
price = models.DecimalField(max_digits=6, decimal_places=2)
rating = models.DecimalField(max_digits=6, decimal_places=1, null=True,
blank=True)
image_url = models.URLField(max_length=1024, null=True, blank=True)
image = models.ImageField(null=True, blank=True)
favourites = models.ManyToManyField(User, related_name='favourites', blank=True)
def __str__(self):
return self.name
i created favourites folder and in the folder i have:
views.py
def favourites(request, product_id):
product = get_object_or_404(Product, pk=product_id)
if product.favourites.filter(id=request.user.ide).exist():
product.favourites.remove(request.user)
else:
product.favourites.add(request.user)
return render(request, 'favourites/product_favourite_list.html')
def product_favourite_list(request):
user=request.user
favourite_products = user.favourites.all()
context = {
'favourite_products': favourite_products
}
return render(request, 'favourites/product_favourite_list.html', context)
urls.py
urlpatterns = [
path('', views.favourites, name='favourites'),
path('favourites/', views.product_favourite_list, name='product_favourite_list'),
]
in product_details i have
def product_detail(request, product_id):
""" A view to show individual product details """
product = get_object_or_404(Product, pk=product_id)
is_favourite = False
if product.favourites.filter(id=request.user.id).exists():
is_favourite = True
context = {
'product': product,
'is_favourite': is_favourite,
}
return render(request, 'products/product_detail.html', context)
product_details HTML has the following links
{% if is_favourite%}
<a href="{% url 'product_favourite_list' id=product.id %}">
<i class="fas fa-heart fa-lg"></i>
</a>
{% else %}
<a href="{% url 'product_favourite_list' id=product.id %}">
<i class="far fa-heart fa-lg"></i>
</a>
{% endif %}
i can see the favourites page. when I click on individual products. I get the error as per the attached snapshot
Hope somebody can help. thanks in advance