Jon
Jon

Reputation: 12864

How to use tornado authentication in a django view?

I use a combination of Django and Tornado for my website. Tornado has a wonderful authentication system and I would like to use it in the django views. Unfortunately I get an AttributeError.

How can I use Tornado authentication in my Django views?

View

import tornado.web
from django import shortcuts

@tornado.web.authenticated
def testpage(request):
    return shortcuts.render(request, 'web/templates/index.html')

Error message

AttributeError: 'WSGIRequest' object has no attribute 'current_user'

Django is connected to Tornado through WSGIContainer:

def main():
    static_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'static')


    assert os.path.isdir(static_path), static_path

    wsgi_app = tornado.wsgi.WSGIContainer(
    django.core.handlers.wsgi.WSGIHandler())

    tornado_app = tornado.web.Application([],static_path=static_path,)
    server = tornado.httpserver.HTTPServer(tornado_app)

    server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

if __name__ == '__main__':
    main()

Upvotes: 0

Views: 546

Answers (1)

Ben Darnell
Ben Darnell

Reputation: 22134

The tornado.auth module goes through the openid/oauth flow, but at the end of that process all it does is call back to your code where you probably set a cookie. You read that cookie in Tornado with get_current_user, but that's specific to the Tornado RequestHandler interface. To use this cookie from django you'll need to read the cookie yourself (see the tornado.web.decode_signed_value function, assuming you set the cookie with set_secure_cookie). You can either find the right place in the django framework to do this, in which case you may be able to use django's login_required decorator, or you can just do it in your django handler in which case you'll need to redirect to the login form on your own.

Upvotes: 1

Related Questions