2014年4月30日水曜日

Pyramid 1.5 + jinja2-alchemy-starter で @view_config()

Pyramid 1.5 で、jinja2-alchemy-starter で作成したプロジェクトを修正します。
pyramid のプロジェクトをつくるときに bin/pcreate -s starter MyProject とか、bin/pcreate -s alchemy MyProject とかするわけですが、どうも jinja2 とうまくいかないことがあります。
そこで jinja2-alchemy-starter  です。
別途インストールする必要がありますが、これでプロジェクトをつくるとデフォで jinja2 になっていて、しかも alchemy 付きになってます。
これは便利なので、これからは jinja2-alchemy-starter  を使おうと思います。

で、今回は前回の jinja2-alchemy-starter でつくったプロジェクトをちょっと修正して、簡単なサンプルをつくってみます。

二つのページをつくって、行ったり来たりできるようにしてみます。テンプレートはひとつにします。
テンプレートの拡張子を .jinja2 だけではなく、.html も使えるようにします。
__init__.py の config.add_view() ではなく、@view_config() を使うように修正します。
  1. py ソースの頭にこれを追加する
    #!/usr/bin/env python
    # coding: UTF-8
    

  2. myproject/__init__.py を修正する
    <修正前>
        config.include('pyramid_jinja2')
        
        #The views/routes are added here
        config.add_static_view('static', 'static')
        
        config.add_route("my_route",'/')
        config.add_view('myproject.views.my_view',
                        route_name="my_route",renderer="mytemplate.jinja2")
        
        return config.make_wsgi_app()
    

    <修正後>
        config.include('pyramid_jinja2')
        config.add_renderer(".html", "pyramid_jinja2.renderer_factory")
    
        #The views/routes are added here
        config.add_static_view('static', 'static')
        
        config.add_route('home', '/')
        config.add_route('next_page', '/next')
        config.scan()
        
        return config.make_wsgi_app()
    

    何を修正したかというと・・・
    config.add_renderer() を追加しました。
    これは、テンプレートのファイル名の拡張子を .jinja2 だけではなく、.html も使えるようにしています。
    config.add_route() で、'/' は views.home()、'/next' で views.next_page() を関連づけました。
     config.scan() は、「@view_config()」を調べて、config.add() してくれます。
    URI とテンプレートファイル名をここで対応づけていましたが、テンプレートファイル名は views.py のその関数のところで対応づけるようになりました。

  3. views.py を修正する。
    インポートの追加。@view_config() を使えるようにします。
    from pyramid.view import view_config
    

    それぞれのページの処理を追加します。最初にあった my_view() は削除します。
    <修正前>
    _ = TranslationStringFactory('MyProject')
    
    def my_view(request):
        session = DBSession()
        #Use session to make queries
        #session.query()
        return {'project':'MyProject'}
    

    <修正後>
    _ = TranslationStringFactory('MyProject')
    
    @view_config(route_name='home', renderer='home.html')
    def home(request):
        page_name = 'home'
        return {'page_name':page_name, 'next_uri':'/next', 'next_name':'next_page'}
    
    @view_config(route_name='next_page', renderer='home.html')
    def next_page(request):
        page_name = 'next_page'
        return {'page_name':page_name, 'next_uri':'/', 'next_name':'home'}
    

  4. テンプレートファイル home.html を追加する
    head は title だけでも良いです。
    <a href= の {{ request.application_url }} はなくても動きます。
    <html><!-- home.html -->
    <head>
        <title>{{ page_name }}</title>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
        <link rel="shortcut icon" href="{{request.application_url}}/static/favicon.ico" />
        <link rel="stylesheet" href="{{request.application_url}}/static/pylons.css" type="text/css" media="screen" charset="utf-8" />
    </head>
    <body>
      This page name is '{{ page_name }}'.<br />
      <a href="{{ request.application_url }}{{ next_uri }}">{{ next_name }}</a> 
    </body>
    </html>
    

次回は、せっかくの alchemy なので、MySQL とつないで、ログインが必要なページをつくってみます。

0 件のコメント:

コメントを投稿