niiyan's blog

niiyanの個人ブログ。

web2py 導入メモ(3): トップページをカスタマイズ

http://localhost:8080/foo/ でアクセスすると表示されるトップページをカスタマイズするところから始めてみる。

日本語で解説している私でも理解できるようなページが見つからなかったので、The Official web2py Book を参考にする。

なお、この時点では web2py の開発環境を使って作業しています。

  • http://127.0.0.1:8000/admin/default/site -> 編集したいアプリの edit ボタンをクリック。
  • Controllers -> default.py の edit をクリック。
    • default.py が URL でコントローラが指定されなかったときに呼び出されるデフォルトのコントローラ。"web2py\applications\avisyntter\controllers\default.py" にある。
    • 同じディレクトリにある appadmin.py は、データベース管理インターフェイス用のコントローラ。今はまだいじらない。
  • index を見ると以下のようになっている。
def index():
   """
   example action using the internationalization operator T and flash
   rendered by views/default/index.html or views/generic.html
   """
   return dict(message=T('Hello World')) 
    • 国際化オペレータ T というのがよくわからないが、どうやらテキストに関わっているっぽい。
    • default/index.html と generic.html もトップページに関連しているらしい。
    • ただし、generic.html には、「このファイルは修正すべきではない」とある。

You should not modify this file.
It is used as default when a view is not provided for your controllers

  • default/index.html を見てみる。
    • 6 行目が default.py の "return dict(message=T('Hello World'))" と連動しているっぽい。
<h1>{{=message}}</h1>
  <li>{{=A(B(T("click here for the administrative interface")), _href=URL('admin','default','index'))}}</li>
    • アプリのタイトルやナビゲーションの部分を変更するには、layout.html を編集するか、2 行目を消して index.html を 1 から作り直す。
{{extend 'layout.html'}}
  • layout.html を見てみる。
    • 109 行目の "{{include}}" の部分に index.html の内容が入る。
      <!-- content -->
     <div id="content" {{=XML(style_content)}} >
       {{include}}
     </div>
     <!-- content -->