2011-07-25 42 views
3

我是Python和Web.py的新手,但我在這個問題上撕裂了我的頭髮。我有一個代碼佈局,在我的網站的根目錄中有我的app.py文件。所有頁面都在一個副導演中,名爲pages。這裏是我的app.py代碼Web.py如何在這種情況下訪問渲染函數

import web 
import page.index 

urls = (
    '/', 'page.index.index', 
) 

render = web.template.render('templates/', base = "layout") # Start the template 
app = web.application(urls, globals()) # Start the app 

if __name__ == "__main__": 
    app.run() 

現在,它執行完美。現在,在index.py文件,這是我的代碼:

class index: 
    def GET(self): 
     testing = 'Hello World' 
     return render.index(testing) 

我得到的錯誤是這樣的:

<type 'exceptions.NameError'> at/
global name 'render' is not defined 
Python /Volumes/Local Disk 2/Work/Casting Board/com/index.py in GET, line 3 
Web  GET http://127.0.0.1:8080/ 

基本上,我試圖訪問功能(或者是方法或類。只是來自PHP,所以不知道從名爲page.index的moucle的末端)render。我怎樣才能解決這個問題?

+0

你確定你不需要使用'import pages'。哪個會尋找頁面文件夾?請參閱https://github.com/alexksikes/mailer/blob/master/application.py,瞭解您嘗試執行的操作。 – KyleWpppd

回答

1

在index.py頁面中,您是否應該包含from web.template import render

+0

根據我的理解,'render = web.template.render('templates /',base =「layout」)'將文件加載爲函數。我可以做你說的,但它沒有所需的功能,因爲它沒有設置。任何幫助? – Colum

+0

看看錯誤消息'global name'render'is not defined'它表示它沒有找到'render'的定義。順便說一句,在你的評論中,你確定要通過做一個任務來重載'render'的定義嗎? –

+0

我想讓'render'註冊爲全局。如果我把索引類放在app.py文件中,這一切都可以正常工作,但只要我把它們分開,它就會中斷。我需要知道一個解決方法,所以我可以從另一個文件調用render.index()。這可能是我不能? – Colum

1

。假定web.template.render()返回一個包含index()方法(我不熟悉web.py)的對象,你需要來告訴Python在哪裏可以找到這個對象。

index.py

import app 

然後:

return app.render.index(testing) 
1

你可以只包括您在app.py文件內的索引類(index.py)。導入Web模塊然後將在代碼的開始處引入函數「render」的定義。