0
我有一個Event
類。活動有presenters
。 presenters
被放置在「參加者」列表中。演講者應該有一個標誌,表明他們實際上是出席者列表中的演講者。即:他們的頭像旁邊的複選標記{% if is_presenter %}
。截至目前,它正在爲所有與會者添加複選標記。我只想顯示presenters
的標誌。我究竟做錯了什麼?如何添加複選標記以僅顯示那些呈現? (另外,我不知道我的頭銜是否適合這種情況,請告訴我)。自定義django模板標記/參數忽略條件
型號:
class Event(models.Model):
title = models.CharField(max_length=200)
presenters = models.ManyToManyField(Profile, null=True, blank=True)
url = models.CharField(max_length=200)
description = models.TextField()
date = models.DateTimeField()
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
tags = models.ManyToManyField(Tag, null=True, blank=True)
class Attendee(models.Model):
event = models.ForeignKey(Event)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
profile = generic.GenericForeignKey('content_type', 'object_id')
瀏覽:
def event(request, id):
event = get_object_or_404(Event, id=id)
is_attending = False
is_presenter = False
if request.user.is_authenticated():
profile = Profile.objects.get(user=request.user)
attendees = [a.profile for a in Attendee.objects.filter(event=event)]
if profile in attendees:
is_attending = True
for presenter in event.presenters.all():
is_attending = True
try:
content_type = ContentType.objects.get(app_label='profiles', model='profile')
Attendee.objects.get(event=event, content_type=content_type, object_id=presenter.id)
is_presenter = True
except Attendee.DoesNotExist:
Attendee(event=event, profile=presenter).save()
模板:
{% for attendee in event.attendees %}
<div class="inline-block">
<a href="/profile/{{ attendee.profile.get_type|lower}}/{{ attendee.profile.user.username }}"{% if attendee.profile.is_presenter %}title="Presenter" class="tooltip-below"{% endif %}>
<img width=30 height=30 src="{% if attendee.profile.avatar %}{% thumbnail attendee.profile.avatar 30x30 crop %}{% else %}{{ DEFAULT_AVATAR }}{% endif %}" />
</a>
{% if is_presenter %}
<i class="icon-ok-sign"></i>
{% endif %}
</div>
{% endfor %}
添加您的解決方案使複選標記消失。這似乎是檢查標記顯示的唯一方法,就是在'try'中有'is_presenter'。我想我要做的是檢查參加者是否是主持人。如果是這樣,顯示標誌(複選標記)。希望能爲您澄清事情,並感謝您的建議。 – Modelesq 2012-07-11 21:16:23