2016-03-12 40 views
2

金字塔應用程序如何在根網址呈現登錄用戶儀表板?未登錄時,根網址顯示錶單中的登錄。python金字塔,登錄時在根網址處呈現用戶儀表板

搜索後我只找到其他框架的例子。

+0

我不知道金字塔好,但我會假設你只是有路由功能中的聲明說:「如果用戶登錄,則渲染此模板,否則,渲染其他模板。」 – haliphax

回答

3

你可以簡單地做:

@view_config(route_name='home', renderer='dashboard.jinja2') 
def home_view(request): 
    if request.authenticated_userid is None: 
     # most people would probably opt to redirect to the login url 
     # here instead of rendering a response, but you asked 
     return render_to_response('login.jinja2', {}, request=request) 
    # user is logged in, so use the dashboard renderer 
    return {} 

然而,金字塔是涼,有謂語。整齊。因此,我們可以使用effective_principals謂詞基於2個不同的視圖之間進行調度用戶是否登錄:

from pyramid.security import Authenticated 

@view_config(route_name='home', effective_principals=Authenticated, renderer='dashboard.jinja2') 
def dashboard_view(request): 
    return {} 

@view_config(route_name='home', renderer='login.jinja2') 
def login_from_home_view(request): 
    return {} 
+0

+1這個更高級的金字塔答案。謂詞是一種更好的方式來保持您的代碼清潔,並使更多的鎖定安全。 IE瀏覽器不能意外地通過條件檢查中的錯誤來授予視圖中的錯誤訪問權限,您的受保護的視圖將永遠不會執行。 –

1

重寫SQLAlchemy + URL dispatch wiki tutorial包含一個示例應用程序,它可以執行您想要的任務,以及密碼散列,授權和測試。這將在Pyramid 1.7中發佈(1.6.1是本文的最新版本)。您可以從本教程的src目錄獲取教程中完整的應用程序源代碼。