1

在 Windows 7 和最新的 webpy 上使用 Python26。

我复制了在 GAE 上设置 Web.py 的基本示例 (http://webpy.appspot.com/),并按照说明编译模板以用于 GAE (http://webpy.org/cookbook/templates_on_gae) ,但这样做之后仍然有 ImportError: No module named templates。

明确一点:有很多人遇到过这个问题,解决方案是编译模板。我这样做了;仍然是同样的错误。

我的实现在这里:https ://bitbucket.org/rhiaro/gae-tutorial(在 webpyworld 目录中)。

我的主文件 code.py 是:

from google.appengine.ext import db
import web

urls = (
'/', 'index',
'/note', 'note',
'/crash', 'crash'
)

render = web.template.render('templates/')

class Note(db.Model):
content = db.StringProperty(multiline=True)
date = db.DateTimeProperty(auto_now_add=True)

class index:
def GET(self):
    notes = db.GqlQuery("SELECT * FROM Note ORDER BY date DESC LIMIT 10")
    return render.index(notes)

class note:
def POST(self):
    i = web.input('content')
    note = Note()
    note.content = i.content
    note.put()
    return web.seeother('/')

class crash:
def GET(self):
    import logging
    logging.error('test')
    crash

app = web.application(urls, globals())

def main():
app.cgirun()

if __name__ == '__main__':
  main() 

按照说明编译模板已在模板文件夹中生成正确的 __ init __.py。但它仍然不会将其识别为模块。

错误输出的最后一部分:

path\to\webpyworld\code.py in ()
8 )
9 
10 render = web.template.render('templates/')
11 
12 class Note(db.Model):
render undefined, web = <module 'web' from 'D:\gaeTut\webpyworld\web\__init__.pyc'>, web.template = <module 'web.template' from 'D:\gaeTut\webpyworld\web\template.py'>, web.template.render = <class web.template.GAE_Render>
path\to\webpyworld\web\template.py in __init__(self=<web.template.GAE_Render instance>, loc='templates/', *a=(), **kw={})
1031         else:
1032             name = loc.rstrip('/').replace('/', '.')
1033             self.mod = __import__(name, None, None, ['x'])
1034 
1035         self.mod.__dict__.update(kw.get('builtins', TEMPLATE_BUILTINS))
self = <web.template.GAE_Render instance>, self.mod undefined, builtin __import__ = <built-in function __import__>, name = 'templates', builtin None = None

<type 'exceptions.ImportError'>: No module named templates 
  args = ('No module named templates',) 
  message = 'No module named templates'
4

1 回答 1

1

您在 app.yaml 中将 templates/ 指定为 static_dir。

这意味着它不适用于应用程序代码,而只会直接响应用户对模板本身的直接请求而提供服务。

于 2012-04-05T11:44:49.943 回答