0

Am trying to integrate Django app with Tornado for some asynchronous request handling (chat). (Reference - https://github.com/thejsj/django-and-rethinkdb)

For that reason, I integrated but couldn't run the tornado_main.py after initiating the Django app in windows (command prompt as admin).

when I run

>c:\python27\python tornado_main.py 

It throws below error

enter image description here

I tried changing different django versions and tried the code as suggested in How use Django with Tornado web server?

Am not sure what is wrong with the configurations that were missing.

Here is my tornado_main.py code-

import django.core.handlers.wsgi
from django.conf import settings
import os
from tornado.options import options, define, parse_command_line
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.wsgi
import tornado.websocket
import tornado.gen
from change_feed import print_changes, SocketHandler, MessageHandler

django.setup()

def main():
  os.environ['DJANGO_SETTINGS_MODULE'] = 'app.settings'
  wsgi_app = tornado.wsgi.WSGIContainer(
    django.core.handlers.wsgi.WSGIHandler()
  )
  current_dir = os.path.dirname(os.path.abspath(__file__))
  static_folder = os.path.join(current_dir, 'static')
  tornado_app = tornado.web.Application([
    ('/new-messages/', SocketHandler),
    ('/messages/', MessageHandler),
    (r'/static/(.*)', tornado.web.StaticFileHandler, { 'path': static_folder }),
    ('.*', tornado.web.FallbackHandler, dict(fallback=wsgi_app)),
  ])
  server = tornado.httpserver.HTTPServer(tornado_app)
  server.listen(8000)
  tornado.ioloop.IOLoop.current().add_callback(print_changes)
  tornado.ioloop.IOLoop.instance().start()

if __name__ == '__main__':
  main()

Am using django 1.8.1 Python 3.4

Could you please suggest how to run this server on windows ?

Here is Settings.py

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '=1gnq3++4*3to$cun@-(f86j_4f^!-k96#f%eej3k$be1o^17o'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app'
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'testTornado.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'testTornado.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = {
    'default': {
        'NAME': 'tornadotest',
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'root',
        'PASSWORD': 'dhyana007',      
    }
}


# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'
LOGGING_CONFIG=None
Community
  • 1
  • 1
Sathish
  • 133
  • 7
  • What in your settings.py? Have you tried set `LOGGING_CONFIG=None`? – Alex Yu Dec 20 '15 at 17:45
  • Hi @Ingaz, I just tried adding LOGGING_CONFIG=None. It gives same error. Could you kindly check the edit? – Sathish Dec 20 '15 at 17:49
  • I think you're missing: `sys.path.append('/home/lawgon/') # path to your project` before setting `os.environ['DJANGO_SETTINGS_MODULE'] = 'app.settings' #name of your application`. – Alex Yu Dec 20 '15 at 17:59
  • Before adding this.. I tried differently, it now throws No Module name 'app' :( – Sathish Dec 20 '15 at 18:06
  • app is my django (INSTALLED_APPS).. When I remove it, tornado doesn't throw any error.. but I cant run my django app :( – Sathish Dec 20 '15 at 18:07
  • Of course. What is the name of your app? After `sys.path.append` it must be `import`-able: as `import your_app` and `import your_app.settings` – Alex Yu Dec 20 '15 at 18:08
  • app name is 'app' itself – Sathish Dec 20 '15 at 18:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98486/discussion-between-ingaz-and-sathish). – Alex Yu Dec 20 '15 at 18:11

0 Answers0