用户可以通过单击添加按钮将该产品添加到观察列表中,然后添加按钮将更改为 Rimo。这次单击此按钮,该产品将从列表中删除。主页上应该有一个链接,点击它会显示监视列表中的所有产品,点击详细按钮可以看到该产品的详细信息,点击删除按钮,您可以将它们从列表中删除。
当我单击按钮时,它向我发送此错误。 怎么了?
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(相关部分):
<form method= "get" action = "{% url 'add' %}">
<button type = "submit" value = {{ product.id }} name = "productid" >Add to
Watchlist</button>
</form>
layout.html(相关部分):
<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>
此文件(layout.html)在页面顶部创建标题,可以通过单击每个标题来访问这些标题。这样,点击关注列表,就会显示列表中的产品。目前,点击此链接不会显示“关注列表中的产品”。
<form method="post" action="{% url 'add' %}"> <button type="submit" value="{{ product.id }}" name="productid">Add to Watchlist</button> </form>
或
product_id = request.GET.get('productid', False)