2010-07-19 52 views
3

使用塔VERSON 1.0: 從主塔書上的FormDemo例如工作:錯誤掛架重定向

http://pylonsbook.com/en/1.1/working-with-forms-and-validators.html

我的控制器具有以下功能:

class FormtestController(BaseController): 

    def form(self): 
     return render('/simpleform.html') 

    def submit(self): 
     # Code to perform some action based on the form data 
     # ... 
     h.redirect_to(controller='formtest', action='result') 

    def result(self): 
     return 'Your data was successfully submitted.' 

首先,我注意到在書中作者指出要導入redirect_to執行以下導入:

from pylons.controllers.util import redirect_to 

這似乎是不正確的,因爲redirect_to的生活路由模塊,所以我把它改成這樣:

from routes import redirect_to 

一切正常,沒有更多的進口錯誤,但是當我執行一個表單提交,我看到了以下回溯


h.redirect_to(controller='formtest', action='result') 
target = url_for(*args, **kargs) 
encoding = config.mapper.encoding 
return getattr(self.__shared_state, name) 
AttributeError: 'thread._local' object has no attribute 'mapper' 

誰能幫助我?

回答

6

嘗試:

from pylons import url 
from pylons.controllers.util import redirect 

# ... 
redirect(url(controller='formtest', action='result')) 

你可能會更好使用當前Pylons 1.0 documentation和更新的1.0 QuickWiki tutorial,網站上的其他參考資料之一。

+1

爲了說明,支持redirect_to(和url_for)已在Pylons 0.97中棄用,並在Pylons 1.0中刪除。 The Pylons book based on 0.97,所以你可能想要閱讀http://pylonshq.com/docs/en/1.0/upgrading/ – 2010-07-19 22:33:30

+0

QuickWiki頁面實際上並沒有完全更新爲Pylons 1.0(它甚至引用了0.97幾個地方)。最好的辦法是鬆鬆地遵循指示,同時查看從bitbucket中提取的quickwiki源代碼 - 該工具適用於1.0。 – rfusca 2010-07-20 17:10:25

+0

這真的很讓人討厭,這本應該涵蓋1.1的書基於0.97 – qliq 2011-11-10 02:53:31