Adding items to wishlist

huangapple go评论76阅读模式
英文:

Adding items to wishlist

问题

以下是翻译的代码部分:

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 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, &#39;products/wishlist.html&#39;)


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


@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(&#39;add_to_wishlist&#39;))

models.py:

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

wishlist.html:

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

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

答案1

得分: 0

add_to_wishlist函数中,您没有将产品添加到愿望清单,只是创建了产品。

尝试使用下面的代码:

@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')

注意:代码已经翻译成中文。

英文:

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(&#39;wishlist&#39;)

huangapple
  • 本文由 发表于 2023年3月21日 01:41:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793566.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定