tornado服务器:未定义Nameerror全局名称

时间:2016-06-16 14:21:34

标签: python html angularjs tornado

我正在从Tornado服务器渲染HTML页面:

@Override
    public void create() {

        (...)

        //particle system stuff
        particleSystem = ParticleSystem.get();
        pointSpriteBatch = new PointSpriteParticleBatch();
        pointSpriteBatch.setCamera(cam);
        particleSystem.add(pointSpriteBatch);

        assets = new AssetManager();
        ParticleEffectLoader.ParticleEffectLoadParameter loadParam =
                new ParticleEffectLoader.ParticleEffectLoadParameter(particleSystem.getBatches());
        ParticleEffectLoader loader = new ParticleEffectLoader(new InternalFileHandleResolver());
        assets.setLoader(ParticleEffect.class, loader);
        assets.load(PARTICLE_PATH, ParticleEffect.class, loadParam);
        assets.finishLoading();

        ParticleEffect originalEffect = assets.get(PARTICLE_PATH);
        particles = originalEffect.copy();
        particles.init();
        particleSystem.add(particles);

    }


    @Override
    public void render() {

        (...)

        int width = Gdx.graphics.getWidth();
        int height = Gdx.graphics.getHeight();

        Gdx.gl.glViewport(0, 0, width, height);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

        modelBatch.begin(cam);
        modelBatch.render(instancesMap.values(), environment);
        renderParticleEffects();
        modelBatch.end();

    }

    /**
     * Renders our particle effects.
     * Must be used between modelBatch.begin and modelBatch.end
     */
    private void renderParticleEffects() {
        particleSystem.update(); // technically not necessary for rendering
        particleSystem.begin();
        particleSystem.draw();
        particleSystem.end();
        modelBatch.render(particleSystem);
    }

我收到以下错误:

class IndexHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    def get(self):
        self.render("neo4j-panama_tornado.html")

class WebSocketHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        print("WebSocket opened")

    def on_message(self, message):
        #print self
        print message
        message_new = message.split('XXXX') 
        keyword_or_query = message_new[0]
        label_type = message_new[1]
        print keyword_or_query
        print node_id
        self.write_message(node_id)

    def on_close(self):
        pass

app = tornado.web.Application([
    (r'/', IndexHandler),
    (r'/websocket', WebSocketHandler),
    (r'/(.+)', web.StaticFileHandler, {'path': 'web_resources/static/'}),
    (r'/(favicon.ico)', web.StaticFileHandler, {'path': 'web_resources/static/img/'}), # TODO: fix favicon (not being found-)
])

if __name__ == '__main__':
    parse_command_line()
    app.listen(options.port)
    tornado.ioloop.IOLoop.instance().start() 

使用AngularJS的HTML代码:

 Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/tornado-4.3-py2.7-cygwin-2.5.1-i686.egg/tornado/web.py", line 1401, in _stack_context_handle_exception
    raise_exc_info((type, value, traceback))
  File "/usr/lib/python2.7/site-packages/tornado-4.3-py2.7-cygwin-2.5.1-i686.egg/tornado/web.py", line 1603, in wrapper
    result = method(self, *args, **kwargs)
  File "server.py", line 120, in get
    self.render("neo4j-panama_tornado.html")
  File "/usr/lib/python2.7/site-packages/tornado-4.3-py2.7-cygwin-2.5.1-i686.egg/tornado/web.py", line 699, in render
    html = self.render_string(template_name, **kwargs)
  File "/usr/lib/python2.7/site-packages/tornado-4.3-py2.7-cygwin-2.5.1-i686.egg/tornado/web.py", line 806, in render_string
    return t.generate(**namespace)
  File "/usr/lib/python2.7/site-packages/tornado-4.3-py2.7-cygwin-2.5.1-i686.egg/tornado/template.py", line 345, in generate
    return execute()
  File "neo4j-panama_tornado_html.generated.py", line 5, in _tt_execute
    _tt_tmp = var.Name  # neo4j-panama_tornado.html:259
NameError: global name 'var' is not defined

上面的HTML代码在独立运行时工作正常,但在通过Tornado渲染时出现错误。

1 个答案:

答案 0 :(得分:0)

Tornado正在尝试将您的HTML文件呈现为模板;因此,它正在查找" var"在模板命名空间中,并没有找到它。您需要为" /"使用静态文件处理程序。代替:

app = tornado.web.Application([
    (r"/?()", web.StaticFileHandler, {'path': 'templates',
                                      'default_filename': 'index.html'}),
])

"()"在URL中是一个"捕获组"。它的原因有点微妙,请参阅the documentation for StaticFileHandler和句子,"请注意,正则表达式中的捕获组需要将路径参数的值解析为get()方法。&# 34;