django模板中没有上下文属性
我正在尝试在Django中的模板中显示特定属性。我相信我已经在上下文中传递了正确的QuerySet,但是由于某种原因,我只能访问ID属性,而不是我需要的标题属性。
views.py:models.py:watchlist.html
@login_required
def watchlist_view(request):
listings = models.WatchedListing.objects.filter(owner = request.user)
return render(request, "auctions/watchlist.html", {"listings": listings})
@login_required
def add_to_watchlist(request, listing_id):
listing = models.Listing.objects.get(pk=listing_id)
user = request.user
currently_watched = models.WatchedListing.objects.filter(owner = request.user)
if listing in currently_watched.all():
messages.error(request, "This item is already on your watchlist.")
return redirect(reverse('listing', args=[listing.pk]))
else:
watchlist = WatchedListing()
watchlist.owner = user
watchlist.watched_listing = listing
watchlist.save()
messages.success(request, "Added to your watchlist!")
return redirect(reverse('listing', args=[listing.pk]))
在模板中我的listing.title
class Listing(models.Model):
title = models.CharField(max_length=64)
description = models.TextField()
start_bid = models.ForeignKey(Bid, on_delete=models.CASCADE, related_name="start_bid")
image_url = models.TextField(null=True)
category = models.CharField(max_length=64, null=True)
current_bid = models.ForeignKey(Bid, on_delete=models.CASCADE, related_name="current_bid")
is_active = models.BooleanField()
owner = models.ForeignKey(User, on_delete=models.CASCADE)
num_bids = models.IntegerField()
created_at = models.DateTimeField(auto_now_add=True)
class WatchedListing(models.Model):
watched_listing = models.ForeignKey(Listing, on_delete=models.CASCADE, related_name='watched_listings', blank=True, null=True)
owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name ='watchlist_owner')
:
{% extends "auctions/layout.html" %}
{% block title %} Watchlist {% endblock %}
{% block body %}
{% if messages %}
{% for message in messages %}
<strong style="color: red">{{ message }}</strong>
{% endfor %}
{% endif %}
{% if listings is not None %}
<ul>
{% for listing in listings %}
<a href = "{% url 'listing' listing_id=listing.id %}">
<li>{{ listing.title }}</li>
</a>
{% endfor %}
</ul>
{% else %}
No watched listings yet.
{% endif %}
{% endblock %}
的位置,我只有空白空间,但是UL子弹列表项目显示了带有适当列表的链接。 HOWEVEVER,如果我更改为listing.id,ID属性将显示。我做错了什么可以让清单的标题出现?
I'm trying to display a specific attribute in a template in Django. I believe I've passed the correct queryset in the context, but for some reason I'm only able to access the id attribute, and not the title attribute that I need.
Views.py:
@login_required
def watchlist_view(request):
listings = models.WatchedListing.objects.filter(owner = request.user)
return render(request, "auctions/watchlist.html", {"listings": listings})
@login_required
def add_to_watchlist(request, listing_id):
listing = models.Listing.objects.get(pk=listing_id)
user = request.user
currently_watched = models.WatchedListing.objects.filter(owner = request.user)
if listing in currently_watched.all():
messages.error(request, "This item is already on your watchlist.")
return redirect(reverse('listing', args=[listing.pk]))
else:
watchlist = WatchedListing()
watchlist.owner = user
watchlist.watched_listing = listing
watchlist.save()
messages.success(request, "Added to your watchlist!")
return redirect(reverse('listing', args=[listing.pk]))
Models.py:
class Listing(models.Model):
title = models.CharField(max_length=64)
description = models.TextField()
start_bid = models.ForeignKey(Bid, on_delete=models.CASCADE, related_name="start_bid")
image_url = models.TextField(null=True)
category = models.CharField(max_length=64, null=True)
current_bid = models.ForeignKey(Bid, on_delete=models.CASCADE, related_name="current_bid")
is_active = models.BooleanField()
owner = models.ForeignKey(User, on_delete=models.CASCADE)
num_bids = models.IntegerField()
created_at = models.DateTimeField(auto_now_add=True)
class WatchedListing(models.Model):
watched_listing = models.ForeignKey(Listing, on_delete=models.CASCADE, related_name='watched_listings', blank=True, null=True)
owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name ='watchlist_owner')
watchlist.html:
{% extends "auctions/layout.html" %}
{% block title %} Watchlist {% endblock %}
{% block body %}
{% if messages %}
{% for message in messages %}
<strong style="color: red">{{ message }}</strong>
{% endfor %}
{% endif %}
{% if listings is not None %}
<ul>
{% for listing in listings %}
<a href = "{% url 'listing' listing_id=listing.id %}">
<li>{{ listing.title }}</li>
</a>
{% endfor %}
</ul>
{% else %}
No watched listings yet.
{% endif %}
{% endblock %}
Where I have listing.title in the template, I just get blank space, but the ul bullet list item shows up with a link to the appropriate listing. Howevever, if I change to listing.id, the id attribute will show up. What am I doing wrong to get the listing's title to show up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在模板中获取
观看
的对象。因此,要在此处拥有标题,您需要通过foreferkey
字段将其引用为You are getting objects of
WatchedListing
in the template. So to have title there, you need to refer it through theForeignKey
field as