I am working on a project and the features is that the user should be able to add the item to their “Watchlist.” If the item is already on the watchlist, the user should be able to remove it and users who are signed in should be able to visit a Watchlist page, which should display all of the listings that a user has added to their watchlist.
Also, when I delete one of the products/listing (still by clicking on the add to watchlist button - i would later improve this), it removes everything in the watchlist (even if there were other items in the watchlist screenshot included).
MODELS.PY
class Watchlist(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
item = models.ManyToManyField(Auction)
def __str__(self):
return f"{self.user}'s watchlist"
VIEWS.PY
def add_watchlist(request, listing_id):
items = Auction.objects.get(pk=listing_id)
watched = Watchlist.objects.filter(user=request.user, item=listing_id)
if watched.exists():
watched.delete()
messages.info(request, 'Successfully deleted from your watchlist')
return render(request, 'auctions/watchlist.html', {'all_watchlist': Watchlist.objects.filter(user=request.user)})
else:
watched, created = Watchlist.objects.get_or_create(user=request.user)
watched.item.add(items)
messages.success(request, 'Successfully added to your watchlist')
return redirect('index')
@login_required
def watchlist(request):
watchlists = Watchlist.objects.all()
context = {'watchlists':watchlists}
return render(request, 'auctions/watchlist.html', context)
It looks like you’re addressing a number of different issues here.
In an attempt to keep me from getting confused among them, I’m going to try and separate them into individual issues.
What I think I understand what you’re talking about first is:
Which I believe is supposed to be handled by this line in your template:
In your template, you’re iterating over the context element named watchlists, which is being created here:
You’re trying to retrieve an attribute named image_url from the Watchlist object, but your watchlist Model as you’ve posted it:
Doesn’t have an attribute named image_url. Therefore, there’s no image to show.
This appears to be the result of this line:
You don’t show your URLs here, or the view that is mapped to the name listing_detail. But from the error message you describe, I’m going to guess that that URL is supposed to give the details of an Auction object. However, the parameter you’re passing to that URL is the PK of a Watchlist object and not the PK of an Auction object.
Correct. You’re deleting the Watchlist object here, not the relationship between a Watchlist and a specific Auction.
You may want to take some time to review the ManyToMany relationship docs, especially the examples, to ensure you’re understanding the APIs that Django uses when working with a many-to-many relationship, and to understand how that type of relationship works within the Django ORM.
This is almost working perfectly right now , the only problem I’m currently having is that when it adds successfully to the watchlist, for e.g the url for the added watchlist is 'http://127.0.0.1:8000/detail/1/, if I delete that watchlist and try to add it again, instead of the url still being the same and showing me the details of the url.
Thank you but it’s still giving me the issue of unique urls, so when I delete a watchlist and try to add it again, I’m still not able to click on it and see the details.
Do I need to delete the previous migration or db.sqlite to effect the changes?
EDIT: Just had to delete and add it again to the watchlist.