2014-01-21 56 views
0

我在的web2py模塊的Python函數來發送e-mails.It有以下代碼web2py中使用的模塊

message = response.render('scheduler/connectionmessage.html',cont) 

我得到的錯誤

<type 'exceptions.NameError'> name 'response' is not defined" 

我怎樣才能讓渲染可用於模塊?目標是在模塊下有幾個這樣的腳本,並通過調度器從控制器下的存根執行它們。 更多的代碼 -

def send_email_invites(): 
    from gluon import * 
    from gluon.template import render 
    db = current.db 
    ......<execute query and populate dictionary> 
    message = response.render('scheduler/connectionmessage.html',cont) 

也差不多了。

+0

你能告訴我們模塊代碼嗎? –

回答

1

您的代碼已包含from gluon import *,這表示您已導入線程本地current對象。該對象包含當前請求的response對象,因此您應該參考current.response而不是response

請注意,模型,控制器和視圖文件中不需要這些文件,因爲這些文件在已包含response對象(以及大部分其他web2py API)的全局環境中執行。

有關更多詳細信息,請參見http://web2py.com/books/default/chapter/29/04/the-core#Accessing-the-API-from-Python-modules

+1

根據Bibhas的建議,我將輸入*改爲膠子輸入電流。這應該沒問題吧? – Jayadevan

+0

只要'current'是你需要的就應該沒問題。實際上,儘管我得到了'import *'的一般性警告,在'from gluon import *'的情況下,它可以獲得整個web2py API,就像您在任何模型,視圖或控制器文件中一樣實際上並沒有導入gluon中的所有單個模塊 - 參見[這裏](https://github.com/web2py/web2py/blob/master/gluon/__init__.py#L13),因此在這個情況可能是好的。當然,如果你真的只是使用API​​中的一個或兩個對象,那麼確定,繼續並明確說明它。 – Anthony

0

調用response.render()

from gluon.globals import Response 
response = Response() 

之前,試試這個,我知道這是很有誘惑力的,但儘量避免from xyz import *和是明確的。

+0

錯誤 - 文件「/var/www/web2py/applications/scheduler/controllers/jobs.py」,第5行,索引 return send_unsent_email_invites.send_email_invites() 文件「applications/scheduler/modules/send_unsent_email_invites.py」,line 25,in send_email_invites message = response.render('scheduler/connectionmessage.html',cont) 文件「/var/www/web2py/gluon/globals.py」,第399行,在渲染中 self._view_environment.update self._vars)。可能是我正在取得進展。 – Jayadevan

+0

這意味着你最初得到的例外已經解決了。這是一個新的例外。如果您自己找不到答案,歡迎您創建一個新問題,您也可以用新的例外更新此問題。 –

+0

雖然這改變了異常,但它並沒有解決最初的問題,它需要訪問與當前請求相關聯的'Response'對象。創建一個新的Response對象將無濟於事。相反,請參閱http://stackoverflow.com/a/21261293/440323。 – Anthony