2012-09-06 39 views
0

現在我有在視圖中一個循環,顯示主題的簡要說明:GAE Python - 從控制器和視圖中分離「文本內容」?

示例代碼:

 <div id="menu"> 
      <ul> 
       {% for item in topics %} 
       <li> 
        <img src="img/{{item.name}}_ico.png" alt="{{item.name}}" /> 
        <h2>{{item.heading}}</h2> 
        <p>{{ item.detail|safe }}</p> 
       </li> 
       {% endfor %} 
      </ul> 
     </div> 

上面的代碼顯示一個圖標 - 一個標題和一些文本的幾個項目。

由於這些主題不會改變,並且僅由開發者更新,我創建了一個包含所有這些內容的控制器的字典..

topics= [ 
        {'name': 'alert', 
        'heading': "Maintain attention", 
        'detail': 'Keep your students involved, alert and attentive during class with closed questions about the current subject.'}, 
        {'name': 'time', 
        'heading': 'Instant feedback', 
        'detail': 'Save precious time and avoid checking quizzes!<br />studyWise&copy; check them instantly, providing you with results.'}, 
        {'name': 'reward', 
        'heading': "Reward students", 
        'detail': 'Motivate students to participate and learn by rewarding good work with positive feedback.'}, 
        {'name': 'money', 
        'heading': 'Save on expenses!', 
        'detail': 'Why spend money buying similar gadgets or subscriptions?<br />Use studyWise&copy; free of charge now.'}, 
        {'name': 'cell', 
        'heading': 'Accessible', 
        'detail': 'Works with any smartphone, laptop or tablet.<br />No installation required.'}, 
        {'name': 'share', 
        'heading': 'Share with colleagues', 
        'detail': 'Share topics, quizes or course statistics with colleagues.<br />See how your assistants are doing.'}, 
        {'name': 'statistics', 
        'heading': 'Statistics', 
        'detail': 'Get helpful info about your students understanding of the learning material today.'} 
       ] 

我所想要做的就是堅持MVC原則,保持代碼清潔。 我應該將這本字典移動到「模型」文件嗎? 這會影響性能嗎?

感謝

回答

1

這些主題應該是數據庫中的記錄,並且應該只對它們進行查詢,當你想使用它們。根據您使用的框架,您可能甚至不需要更改模板。

在Django中你會:

#models.py 
from django.db import models 

class Topic(models.Model): 
    name = models.CharField(...) 
    heading = models.CharField(...) 
    detail = models.TextField() 

#views.py 
from myapp.models import Topic 

# In your view: 
    topics = Topic.objects.all() 

無論他們經常改變或沒有在這方面沒有關係。

我老老實實地認爲這裏的表現影響is not a concern you should have這裏。

相關問題