6

これは私がやろうとしていることに非常に固有なので、それが何であるかを説明し始めます。

  • http:// localhost:6543 / path / to / myplot/plot001.pngのようなプロットを提供するPyramidアプリ
  • プロットが利用できない場合は、別の画像が提供されます(work.png)
  • もう1つの部分は、 http:// localhost:6543 / path / to / myplot / plot001.png?action=editのようなプロットの構成を入力するためのHTMLフォームを提供する deformビューです。ここで、クエリ文字列「action=edit」に注意してください。
  • 構成は、データファイル、テンプレートなどで構成されます。
  • フォームには、保存(構成を保存するため)ボタンとレンダリングボタン(http:// localhost:6543 / path / to / myplot / plot001.png?action = render)があります。結果をpngファイルにレンダリングすると、静的な方法で使用されます。

Matplotlibなどを使用したレンダリングなど、すべての要素を理解しましたが、PyramidとDeformは初めてです。ファイルからのプロットを提供する作業ビューもあります。変形形も作品のようなものです。現時点では、ULRを最適に構成して、サーブ、編集、およびレンダリングのユースケースを区別する方法がわかりません。Pyramidの話では、これはserve_viewとedit_viewのルートを構成する方法を意味していると思います。

__init__.py:
    config.add_route('serve_route', 
        '/{project_name}/testruns/{testrun_name}/plots/{plot_name}.png')
    config.add_route('edit_route', 
        '/{project_name}/testruns/{testrun_name}/plots/{plot_name}.png')
# can I use query strings like "?action=edit" here to distinguish the difference?


views.py:
@view_config(context=Root, route_name='serve_route')
def plot_view(context, request):
... 
@view_config(context=Root, renderer='bunseki:templates/form.pt', route_name='edit_route')
def edit_view(request):
...

ピラミッドのマニュアルでは、ルートにパラメータを設定する方法についてのリファレンスが見つかりませんでした。いくつかのドキュメントやサンプルへのポインタで十分だと思います。詳細は自分で理解できます。ありがとうございました!

4

3 に答える 3

12

コードを分離するために何を好むかに応じて、これを行うには2つの方法があります。

  1. の「if」ステートメントで区切って、すべてのロジックをビューに配置しますrequest.GET.get('action')

    config.add_route('plot', '/{project_name}/testruns/{testrun_name}/plots/{plot_name}.png')
    config.scan()
    
    @view_config(route_name='plot')
    def plot_view(request):
        action = request.GET('action')
        if action == 'edit':
            # do something
            return render_to_response('bunseki:templates/form.pt', {}, request)
    
        # return the png
    
  2. 複数のビューを登録し、Pyramidのビュールックアップメカニズムを使用してそれらの間で委任します。

    config.add_route('plot', '/{project_name}/testruns/{testrun_name}/plots/{plot_name}.png')
    config.scan()
    
    @view_config(route_name='plot')
    def plot_image_view(request):
        # return the plot image
    
    @view_config(route_name='plot', request_param='action=edit',
                 renderer='bunseki:templates/form.pt')
    def edit_plot_view(request):
        # edit the plot
        return {}
    
    # etc..
    

お役に立てれば。これは、単一のURLパターンを登録し、そのURLのさまざまなタイプのリクエストにさまざまなビューを使用する優れた例です。

于 2011-08-11T16:36:09.567 に答える
1

そのような状況で使用できるかどうかはわかりませんcontex=Rootが、あなたが求めているのはおそらくmatchdictです。

__init __。py:

config.add_route('serve_route', 
    '/{project_name}/testruns/{testrun_name}/plots/{plot_name}.png')

views.py:

@view_config(route_name='serve_route')
def plot_view(request):
    project_name = request.matchdict['project_name']
    action = request.params.get('action', None)

http://docs.pylonsproject.org/projects/pyramid/1.1/narr/urldispatch.html#matchdict

編集:

質問がルーティングに関するより一般的な質問である場合は、アクションごとに1つのルートを作成して、ビュー関数のコードをより短く明確に保つ必要があります。たとえば、編集してレンダリングする場合、ルートは次のようになります。

__init __。py:

config.add_route('render_plot',
    '/{project_name}/testruns/{testrun_name}/plots/{plot_name}.png')
config.add_route('edit_plot',
    '/{project_name}/testruns/{testrun_name}/plots/{plot_name}/edit')

views.py:

@view_config('render_plot')
def render(request):
    pass

@view_config('edit_plot', renderer='bunseki:templates/form.pt')
def edit(request):
    pass
于 2011-08-01T13:03:00.257 に答える
0

より効果的な方法は、URLでアクションを指定することです。また、同じルート名または複数のルート名で異なるアクションを提供することもできます。

config.add_route('serve_route', '/{project_name}/testruns/{testrun_name}/plots/{action}/{plot_name}.png')

views.py
@view_config(context=Root, route_name='serve_route', action='view')
def plot_view(request):
    pass

またはクエリ文字列を使用

`config.add_route('serve_route', 
    '/{project_name}/testruns/{testrun_name}/plots/{plot_name}.png')

views.py
@view_config(context=Root, route_name='serve_route')
def plot_view(request):
    try:
       action = getattr(self._request.GET, 'action')
    except AttributeError:
       raise 
于 2016-12-19T16:46:49.263 に答える