2011-08-10 146 views
1

所以我現在剛剛與Django握手,Django模板邏輯

我想要一些相對悲觀的事情。我有這樣一個模型:在我看來

class Article(models.Model): 
    id = models.IntegerField(primary_key=True) 
    volnumber = models.IntegerField(db_column='volNumber') 
    title = models.TextField() 
    keywords = models.TextField(blank=True) 
    start_page = models.IntegerField(null=True, blank=True) 
    end_page = models.IntegerField(null=True, blank=True) 
    author_1 = models.TextField(blank=True) 
    author_2 = models.TextField(blank=True) 
    author_3 = models.TextField(blank=True) 
    author_4 = models.TextField(blank=True) 
    author_5 = models.TextField(blank=True) 
    author_6 = models.TextField(blank=True) 
    author_7 = models.TextField(blank=True) 
    author_8 = models.TextField(blank=True) 
    author_9 = models.TextField(blank=True) 
    author_10 = models.TextField(blank=True) 

def index(request): 
    article_list = Article.objects.all() 
    volume_list = Volume.objects.all() 
    auth_list = ['author_1', 'author_2', 'author_3', 'author_4', 'author_5', 'author_6', 'author_7', 'author_8', 'author_9', 'author_10', ] 

    return render_to_response('index.html', { 
      'article_list': article_list, 
      'volume_list': volume_list, 
      'auth_list' : auth_list, 
    }) 

,我想遍歷第一篇的作者,以及消除對模板列表爲空項:

<ul class="articleList"> 
    {% for article in article_list %} 
     <li> 
     {% for author in auth_list %} 
      <i>{{ article."author" }}</i> 
     {% endfor %} 

     {{ article.title }}{{ article }} 
     </li> 
    {% endfor %} 
</ul> 

很明顯,這不起作用,我不確定模板是否適用於此邏輯,但我希望從中可以明白我正在努力實現的目標。

任何幫助非常感謝。

+1

您確定您的文章有足夠的作者字段?您可能需要更多! – SingleNegationElimination

回答

7

我不確定你想要這樣設計一個作者列表。如果您需要多個作者,請考慮爲作者使用新模型,然後在您的文章類中使用ManyToManyField將文章鏈接到作者。然後,你可以通過在你的模板的作者,像這樣的循環:

{% for author in article.authors.all %} 
    <!-- blah blah --> 
{% endfor %} 

以下是作者模型:

class Author(models.Model): 
    first_name = models.TextField(...) # don't copy to ..., put whatever params you need 
    last_name = models.TextField(...) 

和調整的文章型號:

class Article(models.Model): 
    # add this line 
    authors = models.ManyToManyField(Author) 

現在你不需要在您的查看代碼中返回auth_list

+1

隨着新模型的出現,作者也很容易組織文章。 – eric

+0

嘿,感謝您的輸入。我確實意識到模型看起來不太好,儘管在我的防守中他們是現有數據的傳統模型。我認爲這是值得我努力改變的。感謝您的迅速回答 - 模型設計正確,似乎很簡單。乾杯! –

0

如果你不想改變你的數據模型(儘管我強烈建議你遵循@andrew-lee的建議),你不允許在模板中使用call-by-name屬性。你應該這樣做:什麼已經說過

# in the view 
return render_to_response('index.html', { 
     'article_list': article_list, 
     'volume_list': volume_list, 
     'auth_list' : [getattr(article, name) for name in auth_list], 
}) 

# in the template 
    {% for author in auth_list %} 
     <i>{{ author }}</i> 
    {% endfor %} 
0

大廈,這是很有道理的改變你的模式,但是如果你想保持它的原樣,並僅顯示其具有作者字段列表已填寫,您可以在模板中執行此操作:

<ul class="articleList"> 
    {% for article in article_list %} 
     <li> 
     {% for author in auth_list %} 
      {% if author %} 
      <i>{{ author }}</i> 
      {% endif %} 
     {% endfor %} 

     {{ article.title }}{{ article }} 
     </li> 
    {% endfor %} 
</ul>