The user can add that product to the watch list by clicking on the add button, and then the add button will change to Rimo. And this time by clicking this button, the product will be removed from the list. There should be a link on the main page that by clicking on it, all the products in the watch list will be displayed, and by clicking on the detail button, you can see the details of the product, and by clicking on the remove button, you can remove them from the list.
It sends me this error when I click on the button.
What is wrong?
Page not found (404)
No List matches the given query.
Request Method: GET
Request URL: http://127.0.0.1:8000/add/?productid=1
Raised by: auctions.views.add
Using the URLconf defined in commerce.urls, Django tried these URL patterns, in this
order:
admin/
[name='index']
login [name='login']
logout [name='logout']
register [name='register']
product_detail/<int:product_id>/ [name='product_detail']
watchlist/<str:username> [name='watchlist']
add/ [name='add']
The current path, add/, matched the last one.
views.py:
@login_required(login_url="login")
def watchlist(request, username):
products = Watchlist.objects.filter(user = username)
return render(request, 'auctions/watchlist.html', {'products': products})
@login_required(login_url="login")
def add(request):
product_id = request.POST.get('productid', False)
watch = Watchlist.objects.filter(user = request.user.username)
for items in watch:
if int(items.watch_list.id) == int(product_id):
return watchlist(request, request.user.username)
watch_list = get_object_or_404(List, pk = product_id)
user = request.user.username
context = {
'watch_list': watch_list,
'user': user
}
new_watch = get_object_or_404(Watchlist, context)
new_watch.save()
messages.success(request, "Item added to watchlist")
return product_detail(request, product_id)
@login_required(login_url="login")
def remove(request):
remove_id = request.GET["productid"]
list_ = Watchlist.objects.get(pk = remove_id)
messages.success(request, f"{list_.watch_list.title} is deleted from your
watchlist.")
list_.delete()
return redirect("index")
urls.py:
path("product_detail/<int:product_id>/", views.product_detail,
name="product_detail"),
path('watchlist/<str:username>', views.watchlist, name='watchlist'),
path('add/', views.add, name='add'),
path('remove/', views.remove, name='remove'),
whatchlist.html:
{% extends "auctions/layout.html" %}
{% block body %}
{% if products %}
{% for product in products %}
<img src= {{ item.image.url }} alt = "{{item.title}}"><br>
<a>{{ item.title }}</a><br>
<a>{{ item.category }}</a><br>
<a><a>Frist Bid: </a> {{ item.first_bid }} $ </a><br>
<a href="{% url 'product_detail' item.id %}">View Product</a>
<form action="{% url 'remove' product.id %}" method="post">
{% csrf_token %}
<button type="submit">Remove</button>
</form>
{% endfor %}
{% else %}
<p>No products in watchlist</p>
{% endif %}
{% endblock %}
product_detail.html(Related parts):
<form method= "get" action = "{% url 'add' %}">
<button type = "submit" value = {{ product.id }} name = "productid" >Add to
Watchlist</button>
</form>
layout.html(Related parts):
<li class="nav-item">
<a class="nav-link" href="{% url 'index' %}">Active Listings</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'category' %}">Category</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'watchlist' user.username %}">My
WatchList</a>
</li>
This file(layout.html) creates titles at the top of the page that can be accessed by clicking on each of them. In this way, by clicking on the watch list, the products in the list will be displayed. Currently, by clicking on this link No ‘products in watchlist’ is displayed.
2
Answers
or
product_id = request.GET.get(‘productid’, False)
The reason for your initial error is that
<form method= "get" action = "{% url 'add' %}">
is creating a url with the parameterproductid
, which your path,path('add/', views.add, name='add')
, does not handle.You could change it to
path('add/<int:productid>', views.add, name='add')
and change the view to
def add(request, productid):
Then you wouldn’t need the
product_id = request.POST.get('productid', False)
.The reason you get
"Reverse for 'product_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['product_detail/(?P<product_id>[0-9]+)/\Z']"
is most likely because the line
<button type = "submit" value = {{ product.id }} name = "productid" >Add to Watchlist</button>
has the value unquoted, so it may not read it, thus sending an empty string back.
Change it to
<button type = "submit" value = "{{ product.id }}" name = "productid" >Add to Watchlist</button>