我不會知道sockjs如何使用,但這個例子說明如何在Django模型保存
#IN龍捲風
import brukva
import tornado.web
import tornado.websocket
c = brukva.Client()
c.connect()
class MessagesHandler(tornado.websoket.WebsocketHandler):
def open(self):
#....
def handle_request(self, response):
pass
def on_message(self, message):
#....
c.publish(self.channel, json.dumps({
"sender": self.sender_name,
"text": message,
}))
http_client = tornado.httpclient.AsyncHTTPClient()
request = tornado.httpclient.HTTPRequest(
'/to/django_project/url/chat_save_base/',
method="POST",
body=urllib.urlencode({
"message": message.encode("utf-8"),
"sender": self.sender.name,
})
http_client.fetch(request, self.handle_request)
#IN django_url
url(r'/to/django_project/url/chat_save_base/','app.my_view')
#my_view
from django.views.decorators.csrf import csrf_exempt
from messages.models import Message
@csrf_exempt
def my_view(request):
message_text = request.POST.get("message")
sender = User.objects.get(id=request.POST.get("sender_id"))
message = Message()
message.text = message_text
message.sender_id = sender_id
message.save()
其他信息來源: http://habrahabr.ru/post/160123/
你也有python 3解決方案麼? – Alp 2013-12-23 10:25:29
我沒有抱歉,( – madjardi 2013-12-24 13:02:29