试图添加一个项目到我的监视列表突然开始返回一个错误
没有与给定查询匹配的监视列表.
下面是我的代码.
models.py个
class Listing(models.Model):
product_name = models.CharField(max_length=64, verbose_name="product_name")
product_description = models.TextField(max_length=200, verbose_name="product description")
product_image = models.ImageField(upload_to="images/", verbose_name="image", blank=True)
is_active = models.BooleanField(blank=False, default=True)
initial_price = models.DecimalField(decimal_places=2, max_digits=6, default=False)
owner = models.ForeignKey(User, related_name="auction_owner", on_delete=models.CASCADE, default=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="category")
def __str__(self):
return self.product_name
class Watchlist(models.Model):
user = models.OneToOneField(User, on_delete=models.DO_NOTHING)
items = models.ManyToManyField(Listing, null=True, related_name="watchlist_item")
views.py个
def add_to_watchlist(request, item_id):
item = get_object_or_404(Listing, pk=item_id)
user_watchlist = get_object_or_404(Watchlist, user=request.user)
user_watchlist.items.add(item)
return redirect("watchlist")
urls.py
path("add_to_watchlist/<int:item_id>", views.add_to_watchlist, name="add_to_watchlist"),
template个
<div class="btn-groups">
{% if listing in watchlist_items %}
<a href="{% url 'remove_from_watchlist' item_id=listing.id %}" class="fas fa-shopping-cart"><button type = "button" class = "add-cart-btn">remove from watchlist</button></a>
{% else %}
<a href="{% url 'add_to_watchlist' item_id=listing.id %}" class="fas fa-shopping-cart"><button type = "button" class = "add-cart-btn">add to watchlist</button></a>
{% endif %}
</div>