GAE-Python-separating';文本内容';从控制器和视图?
现在,视图中有一个循环,显示主题的简要说明: 示例代码:GAE-Python-separating';文本内容';从控制器和视图?,python,model-view-controller,google-app-engine,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}}" />
<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>
{主题%中项目的%s}
-
{{item.heading}}
{{item.detail | safe}}
{%endfor%}
上面的代码显示一个图标-一个标题和几个项目的一些文本
由于这些主题永远不会更改,只由开发人员更新,因此我在控制器中创建了一个包含所有这些内容的字典
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© 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© 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.'}
]
主题=[
{'name':'alert',
‘标题’:‘保持注意’,
“细节”:“在课堂上,让你的学生参与进来,保持警觉和专注,就当前主题提出封闭式问题。”,
{'name':'time',
“标题”:“即时反馈”,
“细节”:“节省宝贵的时间,避免检查测验!
studyWise©;立即检查测验,为您提供结果。”,
{'name':'raward',
‘标题’:‘奖励学生’,
“细节”:通过积极反馈奖励优秀作品,激励学生参与学习,
{'name':'money',
“标题”:“节省开支!”,
“详细信息”:“为什么要花钱购买类似的小工具或订阅?
现在就免费使用studyWise©;”,
{'name':'cell',
“标题”:“可访问”,
“详细信息”:“适用于任何智能手机、笔记本电脑或平板电脑。
无需安装。”,
{'name':'share',
“标题”:“与同事分享”,
“细节”:“与同事分享主题、测验或课程统计。
看看你的助手们做得怎么样。”,
{'name':'statistics',
'标题':'统计',
“详细信息”:获取有关您的学生今天对学习材料理解的有用信息。}
]
我想做的是坚持MVC原则,保持代码整洁。
我应该把这本字典移到一个“模型”文件中吗?
这会影响性能吗
谢谢这些主题应该是数据库记录,您应该在想要使用它们时查询它们。根据您使用的框架,您甚至可能不需要更改模板 在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()
在这方面,他们是否经常改变并不重要
老实说,我认为这里的性能影响很大