How to redirect to another view with arguments and 'cannot unpack non-iterable Listing object' django
Essentially what I am trying to do is allow a user to delete their own comments and simply refresh the page with all the data with that page loaded in from my 'view' view. However, I'm not sure about the best way to go about this as my view function has multiple variables in its context that I cannot access from my 'delete_comment' view. Also I keep running into an error when I try to access a given listing through a foreign key relationship and I get the error 'cannot unpack non-iterable Listing object' even though there is a listing associated with each comment. Here is everything associated with what I am trying to accomplish.
views.py
def view(request, listing_title):
listing = Listing.objects.get(title=listing_title)
# If someone is logged in
user = request.user
if user.id != None:
try:
watchlist = PersonalWatchList.objects.get(user=request.user)
watchlist_listings = watchlist.listings.all()
except:
watchlist = None
watchlist_listings = None
return render(request, "auctions/listing.html", {
"listing": listing,
"watchlist": watchlist,
"watchlist_listings": watchlist_listings,
"comments": Comment.objects.filter(item=listing).order_by('-date_created')
})
else:
return render(request, "auctions/listing.html", {
"listing": listing,
"comments": Comment.objects.filter(item=listing).order_by('-date_created')
})
def delete_comment(request, comment_id):
comment_details = Comment.objects.get(id=comment_id)
# item is None? Supposed to be of type 'Listing'
auction = comment_details.item
listing = Listing.objects.get(auction)
listing_title = listing.title
Comment.objects.get(id=comment_id).delete()
# Need some advice on this line
return redirect(f'/view/{listing_title}')
Snippet of urls.py
path("view/<str:listing_title>", views.view, name="view"),
path("delete_comment/<int:comment_id>", views.delete_comment, name="delete_comment")
models.py
class Comment(models.Model):
comment = models.CharField(max_length=64)
item = models.ForeignKey('Listing', on_delete=models.CASCADE, null=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
date_created = models.DateTimeField(auto_now=True)
def __str__(self):
return f"{self.comment}"