0

現在、トピックの簡単な説明を表示するループがビューにあります。

サンプルコード:

        <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の原則を順守し、コードをクリーンに保つことです。この辞書を「モデル」ファイルに移動する必要がありますか?それはパフォーマンスに影響しますか?

ありがとう

4

1 に答える 1

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()

それらが頻繁に変わるかどうかは、その点では重要ではありません。

正直なところ、パフォーマンスへの影響はここでの懸念事項ではないと思います。

于 2012-09-06T21:52:07.903 に答える