2011-08-01 138 views
6

這是非常具體的什麼,我試圖這樣做,我開始描述它是什麼:使用查詢字符串

  • 金字塔應用中投放情節像http://localhost:6543/path/to/myplot/plot001.png
  • 如果情節不可用另一個圖像被提供(work.png)
  • 另一部分是變形視圖,它提供了一個HTML表單來輸入配置,例如:http://localhost:6543/path/to/myplot/plot001.png?action=edit。請注意查詢字符串「action = edit」。
  • 配置由數據文件,模板等組成
  • 窗體具有save(保存配置)和渲染按鈕(http:// localhost:6543/path/to/myplot/plot001.png?action = render )。將結果呈現爲一個png文件,然後以靜態方式使用。

我想出了所有使用Matplotlib等渲染的東西,但我是Pyramid和Deform的新手。我也有一個工作視圖,用於從文件中提供圖表。變形也是一種作品。目前我還不清楚如何最好地構建ULR來區分服務,編輯和呈現用例。我猜在金字塔這裏說這意味着如何配置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): 
... 

我金字塔手冊我找不到參考如何在路線中設置參數。我想一個指向一些文檔或樣本的指針就足夠了,我可以自己弄清楚細節。謝謝!

+0

金字塔有他們稱之爲Multidict的機制。我認爲這是訪問查詢字符串的方式。我將合併兩個視圖的情節和編輯成一個,並使用如下所示的區別: if request.GET.getall('action')中的'編輯': #編輯配置 – mark

+0

你在說什麼上面的評論是好的,這可能是我會這樣做的方式;您可以將自定義謂詞交替添加到add_route語句中,以便匹配查詢字符串中的兩個路由。請參閱http://docs.pylonsproject.org/projects/pyramid/1.1/narr/urldispatch.html#custom-route-predicates –

回答

11

有兩種方式這取決於你想要分離你的代碼。

  1. 將所有的邏輯放入您的視圖中,由request.GET.get('action')上的'if'語句分隔。它們之間

    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. 註冊多個視圖和委託使用金字塔的看法查找機制。

    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上的不同類型的請求使用不同的視圖。

+0

request_param ='action = edit'它是!我對此感到興奮。金字塔太棒了! – mark

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

編輯:

如果您的問題是關於多路由一個一般性的問題,你應該創建o ne行動,以保持您的視圖功能的代碼更短,更清晰。例如,如果要編輯和渲染,你的路由可能是這個樣子:

__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 
+1

我不認爲matchdict是URL參數的選擇。參數沒有定義的順序。我的意思是如果你有多個參數你可以互換它們。就我理解的匹配而言,它意味着覆蓋「?」左側的URL部分。 – mark

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