Ticksy
Ticksy

Reputation: 561

Access database connection from UI Module in Tornado

How can I get access to the database connection in the UI Module? I'm using built-in wrapper for MySQL in Tornado.

My application is completely similar this

Upvotes: 2

Views: 553

Answers (2)

Patrix Zheng
Patrix Zheng

Reputation: 51

Just map the module class, don't use a separated uimodule.py. Like this:

class EntryModule(tornado.web.UIModule):
    def render(self, entry):
        return self.render_string('modules-entry.html', entry=entry)

settings = {
    "login_url": "/login",
    ui_modules={"Entry": EntryModule},
}

application = tornado.web.Application([
    (r"/", HomeHandler),
    (r"/entry/([0-9]+)", EntryHandler),
    ], **settings)

Notice : ui_modules={"Entry": EntryModule}

Upvotes: 2

Ticksy
Ticksy

Reputation: 561

I used self.handler.db. It works.

Upvotes: 1

Related Questions