I am trying to display the watchlisted items of the logged in user. I am able to display a list but the item details such as title, price etc. don't show up.
models.py:
class Watchlist(models.Model):
user = models.ForeignKey(User, on_delete = models.CASCADE, name = "user")
item = models.ForeignKey(AuctionItem, on_delete = models.CASCADE, name = "item")
def __str__(self):
return f"Username: {self.user} / {self.item} "
views.py:
@login_required
def watchlist(request,user):
user = request.user
item_list = Watchlist.objects.filter(user_id=user)
return render(request, "auctions/watchlist.html",{
"item_list" : item_list
})
watchlist.html:
{% extends "auctions/layout.html" %}
{% block body %}
{% for item in item_list %}
<div class = "frame">
{% if item.image %}
<img src="{{item.image}}" style= "width: 30vw;">
{% endif %}
<h4><a href="{% url 'listing' item.id %}">{{item.title}}</a></h4>
<div id="text"><strong>Price:</strong> ${{item.price}}</div>
<div id="text"><strong>Description:</strong> {{item.description}}</div>
<div id="text"><strong>Category:</strong> {{item.category}}</div><br>
<div id="date">Created {{item.date}}</div>
</div>
{% endfor %}
{% endblock %}
urls.py:
urlpatterns = [
path("", views.index, name="index"),
path("login", views.login_view, name="login"),
path("logout", views.logout_view, name="logout"),
path("register", views.register, name="register"),
path("create", views.create_listing, name="create"),
path("listing/<int:listing_id>", views.listing, name = "listing"),
path("<str:user>/watchlist", views.watchlist, name= "watchlist")
]
and here is what I'm getting as a result:
CodePudding user response:
In view:
item_list = AuctionItem.objects.filter(watchlist__user=user)
The template stays untouched.
Or only in template in forloop it should be {{item.item.price}}
because item alone is Watchlist object.