2012-07-11 108 views
0

我有一个Event类。活动有presenterspresenters被放置在“参加者”列表中。演讲者应该有一个标志,表明他们实际上是出席者列表中的演讲者。即:他们的头像旁边的复选标记{% 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 %} 

回答

0

我不正确的理解你的问题。但似乎问题在于你的is_presenter标志不是你的对象的属性。因为Python允许dinamically创建属性,也许,解决方案可能是这个布尔VAR设置为这样的模式:

... 
    if profile in attendees: 
     profile.is_attending = True 
for presenter in event.presenters.all(): 
    presenter.is_attending = True 

模板:

{% if profile.is_presenter %} 

*优雅的解决方案是使用is_presenter()方法扩展配置文件模型。*

+0

添加您的解决方案使复选标记消失。这似乎是检查标记显示的唯一方法,就是在'try'中有'is_presenter'。我想我要做的是检查参加者是否是主持人。如果是这样,显示标志(复选标记)。希望能为您澄清事情,并感谢您的建议。 – Modelesq 2012-07-11 21:16:23