1👍
✅
That’s because for that user, no Watchlist
was added yet, you can create one if missing with:
def add_to_watchlist(request, item_id):
item = get_object_or_404(Listing, pk=item_id)
user_watchlist, __ = Watchlist.objects.get_or_create(user=request.user)
user_watchlist.items.add(item)
return redirect('watchlist')
Note: Section 9 of the HTTP protocol
specifies that requests like GET and HEAD should not have side-effects, so you
should not change entities with such requests. Normally POST, PUT, PATCH, and
DELETE requests are used for this. In that case you make a small<form>
that
will trigger a POST request, or you use some AJAX calls. Some browsers look for
links and exhaustively already cache the responses of such links, so these might
trigger such views with side-effects.
Source:stackexchange.com