GAE Python - 将“文本内容”与控制器和视图分开?

时间:2012-09-06 21:42:33

标签: python model-view-controller google-app-engine

现在我在视图中有一个循环,显示主题的简短描述:

示例代码:

        <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 个答案:

答案 0 :(得分: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