[Answered ]-Django โ€“ add to favourites button in ListView

1๐Ÿ‘

โœ…

You are sending your slug not in a ListView you are sending it

 <form action="{% url 'products_in_favourites' product.slug %}" method="POST" class="mx-3"> 

to products_in_favourites function, and doing it wrong at recieving part,

You are not sending it to a function as slug, you are sending it as data with post reqeust, and so you should recieve it same way:

@login_required
def products_in_favourites(request):
    #print all data to see what is coming to this view
    print(request.POST)
    slug = request.POST['product_slug']
    product = get_object_or_404(Product, slug=slug)

Also it is better to pass pk (primary key) instead of slug, primary key has indexes by default in database, so your query will be much much faster if you use pk.

๐Ÿ‘คoruchkin

Leave a comment