2013-09-21 53 views
2

例一AttributeError的:「瓶」對象有沒有屬性「模板」

考慮以下幾點:

import bottle 
import pymongo 

application = bottle.Bottle() 
@application.route('/') 

def index(): 

    cursor = [ mongodb query here ] 

    return application.template('page1',{'dbresult':cursor['content']}) 

假設MongoDB的查詢是正確的,並且應用程序調用contentcursor正確,並將其傳遞到格式正確的模板。

我在日誌中獲得的錯誤是用能夠使用的template()方法如做:如果我改變了相應的分配和調用

AttributeError: 'Bottle' object has no attribute 'template' 

例二

application = bottle 
application.template 

的錯誤是:

TypeError: 'module' object is not callable 

例三

如果我改變了相應的分配和調用:

application = bottle 
@application.route('/') 
@application.view('page1.tpl') 

return {'dbresult':cursor['content']} 

的錯誤是:

TypeError: 'module' object is not callable 

問題

使用template()方法獲得Example One的正確方法是什麼?

回答

1

爲了得到 「實施例一」 工作:

return bottle.template('page1',{'dbresult':cursor['content']}) 

template()bottle模塊中;只需將其引用爲bottle.template(...)即可。

1

bottle.template()不是bottle.Bottle()應用程序對象的方法。這是bottle模塊中的一項功能。

相關問題