英文:
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, '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.
答案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('wishlist')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论