Adding items to wishlist

Question:

so Im trying to work on adding a wishlist item to the wishlist page. So Im creating a view to add a wishlist to the page and a view to show the items ive added. But the issues ocurs when i press the add to wishlist button on a product.The item i added is not showing up in the wishlist page for some reason i cant seem to figure out.

Here is my code below.

views.py:

@login_required
def wishlist(request):
    wishlist, created = Wishlist.objects.get_or_create(user=request.user.userprofile)
    return render(request, 'products/wishlist.html')


@login_required
def add_to_wishlist(request, product_id):
    product = Product.objects.get(id=product_id)
    return redirect('wishlist')


@login_required
def remove_from_wishlist(request, product_id):
    product = get_object_or_404(Product, pk=product_id)
    wishlist = Wishlist.objects.get(user=request.user.userprofile)
    wishlist.products.remove(product)
    return HttpResponseRedirect(reverse('add_to_wishlist'))

models.py:

class Wishlist(models.Model):
    user = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
    product = models.ManyToManyField(Product, blank=True)

wishlist.html:

{% block content %}
<div class="overlay"></div>
    <div class="container">
        <h1>My Wishlist</h1>
        <div class="row">
            {% if wishlist.products.all %}
                {% for product in wishlist.products.all %}
                    <div class="col-md-4">
                        <div class="card mb-4 shadow-sm">
                            <div class="card-body">
                                <h2 class="card-title">{{ product.name }}</h2>
                                <p class="card-text">{{ product.description }}</p>
                                <p class="card-text">$ {{ product.price }}</p>
                                <form action="{% url 'remove_from_wishlist' product.id %}" method="POST">
                                    {% csrf_token %}
                                    <button class="btn btn-danger" type="submit">Remove from Wishlist</button>
                                </form>
                            </div>
                        </div>
                    </div>
                {% endfor %}
            {% else %}
                <p>Your wishlist is empty.</p>
            {% endif %}
        </div>
    </div>
{% endblock %}

So ive tried changing the view to be able to show the products added, but ive had no luck so far.

Asked By: Mustafa Naji

||

Answers:

In the add_to_wishlist function, you’re not adding the product into wishlist, you just created the product.

Try below code instead

@login_required
def add_to_wishlist(request, product_id):
    wishlist = Wishlist.objects.get(user=request.user.userprofile)
    product = Product.objects.get(id=product_id)
    wishlist.product.add(product)
    wishlist.save()
    return redirect('wishlist')
Answered By: Komal Gupta
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.