提问人:zahra shokrizadeh 提问时间:11/18/2023 最后编辑:zahra shokrizadeh 更新时间:11/19/2023 访问量:100
此错误的原因:没有列表与给定查询匹配
Cause of this error: No List matches the given query
问:
用户可以通过单击添加按钮将该产品添加到观察列表中,然后添加按钮将更改为 Rimo。这一次,通过单击此按钮,该产品将从列表中删除。 主页上应该有一个链接,点击它,就会显示观察列表中的所有产品,点击详情按钮,可以看到产品的详细信息,点击删除按钮,可以将它们从列表中删除。
models.py
class User(AbstractUser):
pass
class List(models.Model):
choice = (
('d', 'Dark'),
('s', 'Sweet'),
)
user = models.CharField(max_length=64)
title = models.CharField(max_length=64)
description = models.TextField()
category = models.CharField(max_length=64)
first_bid = models.IntegerField()
image = models.ImageField(upload_to="img/", null=True)
image_url = models.CharField(max_length=228, default = None, blank = True, null =
True)
status = models.CharField(max_length=1, choices= choice)
active_bool = models.BooleanField(default = True)
class Watchlist(models.Model):
user = models.CharField(max_length=64)
watch_list = models.ForeignKey(List, on_deleted=
models.CASCADE)
views.py:
def product_detail(request, product_id):
product = get_object_or_404(List, pk=product_id)
comments = Comment.objects.filter(product=product)
if request.method == 'POST':
# comment
if 'comment' in request.POST:
user = request.user
if user.is_authenticated:
content = request.POST.get('content')
comment = Comment(product=product, user=user, content=content)
comment.save()
context = {
'product': product,
'comments': comments,
}
return render(request, 'auctions/product_detail.html', context)
@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, productid):
#watch = Watchlist.objects.filter(user =
#request.user.username)
watchlist_product = get_object_or_404(Watchlist,
pk=productid)
for items in watchlist_product:
if int(items.watch_list.id) == int(productid):
return watchlist(request, request.user.username)
product = get_object_or_404(List,
pk=watchlist_product.watch_list)
new_watch = Watchlist(product, user = request.user.username)
new_watch.save()
messages.success(request, "Item added to watchlist")
return product_detail(request, productid)
@login_required(login_url="login")
def remove(request, pid):
#remove_id = request.GET[""]
list_ = Watchlist.objects.get(pk = pid)
messages.success(request, f"{list_.watch_list.title} is
deleted from your watchlist.")
list_.delete()
return redirect("index")
product_deyail.html(仅与问题相关的部分):
<form method= "get" action = "{% url 'add' product.id %}">
<button type = "submit" value = "{{ product.id }}" name
= "productid" >Add to Watchlist</button>
</form>
监视列表.html:
{% extends "auctions/layout.html" %}
{% block body %}
{% if products %}
{% for product in products %}
<img src= {{ product.image.url }} alt = "
{{product.title}}"><br>
<a><a>Product:</a>{{ product.title }}</a><br>
<a><a>Category: </a>{{ product.category }}</a><br>
<a><a>Frist Bid: </a> {{ product.first_bid }} $ </a>
<br>
<a href="{% url 'product_detail' product.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 %}
layout.html(仅与问题相关的部分 & 显示链接名称):
<ul class="nav">
<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 'watchlist'
user.username %}">My WatchList</a>
</li>
{% if user.is_authenticated %}
<li class="nav-item">
<a class="nav-link" href="{% url 'logout' %}">Log Out</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="{% url 'login' %}">Log In</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'register' %}">Register</a>
</li>
{% endif %}
urls.py:
path('watchlist/<str:username>', views.watchlist,
name='watchlist'),
path('add/<int:productid>', views.add, name='add'),
path('remove/<int:pid>', views.remove, name='remove'),
错误: 反转“product_detail”,未找到参数“('',)”。尝试了 1 种模式: ['product_detail/(?P<product_id>[0-9]+)/\Z']
监视列表.html:
<a href="{% url 'product_detail' product_id %}">View Product</a>
答:
<form method="post" action="{% url 'add' %}"> <button type="submit" value="{{ product.id }}" name="productid">Add to Watchlist</button> </form>
或
product_id = 请求。GET.get('productid', 假)
评论
初始错误的原因是,使用参数 创建 url,您的路径 ,无法处理该参数。<form method= "get" action = "{% url 'add' %}">
productid
path('add/', views.add, name='add')
您可以将其更改为
path('add/<int:productid>', views.add, name='add')
并将视图更改为
def add(request, productid):
那么你就不需要 .product_id = request.POST.get('productid', False)
你得到的原因
"Reverse for 'product_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['product_detail/(?P<product_id>[0-9]+)/\\Z']"
很可能是因为这条线
<button type = "submit" value = {{ product.id }} name = "productid" >Add to Watchlist</button>
值未加引号,因此它可能不会读取它,从而发回空字符串。
将其更改为
<button type = "submit" value = "{{ product.id }}" name = "productid" >Add to Watchlist</button>
评论
{% for product in products %}
item.id
products
{% for product in products %}
product
product.id
product.title
item
pid
item.id
product.id
pid
product.id
评论