django模板中没有上下文属性

发布于 2025-02-08 16:01:52 字数 2702 浏览 1 评论 0原文

我正在尝试在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池木 2025-02-15 16:01:52

您正在模板中获取观看的对象。因此,要在此处拥有标题,您需要通过foreferkey字段将其引用为

<li>{{ listing.watched_listing.title }}</li>

You are getting objects of WatchedListing in the template. So to have title there, you need to refer it through the ForeignKey field as

<li>{{ listing.watched_listing.title }}</li>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文