The button should be a small vertical rectangle and the top width should stick out a little. It should also be white before pressing and crimson after pressing.
And when the pointer goes over this button, the text "Add" and when deleting, "Remove" should be displayed.
However, it’s just a rectangle, and even its name doesn’t change from addition to deletion (the color and style don’t change either).
Thanks in advance for your ideas:)
product_detail.html:
<form method= "get" action = "{% url 'add' product.id %}">
{% csrf_token %}
<button clasa="bottonwatch" type = "submit" value = "{{ product.id }}" name = "productid">
<div class="addwatch">
{% if product.watch_list %}
addWatchlist
</div>
<div class="removewatch">
{% else%}
remove
</div>
{% endif %}
</button>
</form>
Style:
.bottonwatch {
border-radius: 50%;
height: 2.5rem;
min-width: auto;
padding: 0;
width: 2.5rem;
}
.addwatch {
color: black;
background-color: white;
}
.remove{
color: white;
background-color: rgb(100, 36, 36);
}
views.py:
@login_required(login_url="login")
def add(request, productid):
product_id = request.GET.get('productid', False)
watch = Watchlist.objects.filter(user = request.user.username)
for items in watch:
if (items.watch_list.all) == int(productid):
return watchlist(request, request.user.username)
else:
return remove(request, productid)
new_watch = Watchlist(product_id, user=request.user.username)
new_watch.save()
messages.success(request, "The product has been added to your WatchList.")
return product_detail(request, productid)
@login_required(login_url="login")
def remove(request, pid):
list_ = get_object_or_404(Watchlist, pk = pid)
messages.success(request, "The product was removed from your WatchList.")
list_.delete()
return redirect("index")
2
Answers
you need to structure your button text differently based on the conditions.
correct the CSS class names in both your HTML and CSS. The structure should be a span element within the button to allow the dynamic change of text based on conditions. Additionally, apply styles to the span with class addwatch to change background and text colors.
I saw some problems in the problem you raised that might help:
{% if product.watch_list %} <div class="addwatch"> addWatchlist </div> <div class="remove"> {% else%} remove </div> {% endif %}
I hope it is useful.