title: How to insert data from a tornado web-socket to a mySQL database using Django
body: <p>I am using Django with mysql to create a monitoring web page. I have also created a web-socket server with tornado. What I want to do is to insert data in mysql database every time I receive something from the web-socket.</p>
  
  <p>What kind of connections I should do between Tornado and the database? It's better to send the data to Django and then make the inserts into the database or create a python program for doing that? </p>
  
  <p>I am quite lost in how exchange data between django and tornado.</p>
  
  <p><strong>After the answer of EvilX:</strong></p>
  
  <p>I have create a function to insert the data in the database in tornado server file but I receive an error:</p>
  
  <pre><code>django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
  </code></pre>
  
  <p>The tornado server file Is like this:</p>
  
  <pre><code>import tornado.httpserver
  import tornado.websocket    
  import tornado.ioloop    
  import tornado.web
  import json
  from storage.models import System, Flywheel, Sensor, SensorData
  from threading import Thread
  import os
  
  
  def dataInsert():  
  
  string='{"jSonString":value}'
  
  jdata=json.loads(string)
  
  for system_name in jdata.keys():
      system = System.objects.get(name= system_name)
      for flywheel_name in jdata[system_name].keys():
          flywheel=Flywheel.objects.get(system=system.id,name=flywheel_name)
  
          for sensor_name in jdata[system_name][flywheel_name].keys():
  
              sensor= Sensor.objects.get(flywheel=flywheel.id,sensorType=sensor_name)
  
              sd=SensorData(sensor=sensor, value= jdata[system_name][flywheel_name][sensor_name])
              sd.save()
  
  
  
  
  
  class WSHandler(tornado.websocket.WebSocketHandler):
  def check_origin(self, origin):
      return True
  def open(self):
      print 'new connection'
      self.write_message("Hello World")
  
  
  def on_message(self, message):
      #self.write_message(u"Your message was: " + message)
      #print 'message received %s' % message
  
  def on_close(self):
    print 'connection closed'
  
  application = tornado.web.Application([
  (r'/ws', WSHandler),])
  
  
  
  if __name__ == "__main__":  
  os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' # TODO: edit this
  #sys.path.append('./demosite') # path to your project if needed
  
  thread = Thread(target=dataInsert())
  thread.start()
  thread.join()
  
  http_server = tornado.httpserver.HTTPServer(application)  
  
  http_server.listen(6666)
  
  tornado.ioloop.IOLoop.instance().start()
  </code></pre>