2010-01-27 73 views
4

Django的測試服務器,因爲我寫它採用雙絞線網絡的服務請求的異步和Django的正常內容交付的應用程序,我認爲這將是不錯的通過相同的扭曲反應器下運行Django的WSGI接口。運行下扭曲網

我也想用Django提供的漂亮的測試服務器工具來測試我的應用程序。起初,我只是簡單地創建了測試數據庫並在反應堆下觸發了WSGIHandler,但是這並不起作用,因爲WSGIHandler在初始化過程中沒有看到創建的測試數據庫。

因此,我決定周圍寫工作並具有分貝創建並裝載在第一請求,這是細用於測試服務器裝置。下面是我使用(精簡)腳本:

import os, sys 
import django.core.handlers.wsgi 

from django.core.management import call_command 
from django.db import connection 

from twisted.web.wsgi import WSGIResource 
from twisted.internet import reactor 
from twisted.web.server import Site 

sys.path.append('/path/to/myapp') 
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings' 

_app = django.core.handlers.wsgi.WSGIHandler() 
initialized = False 
fixtures = (...) # Put your fixtures path here 

def app(e,sr): 
    global initialized 

    if not initialized: 
    connection.creation.create_test_db(verbosity=1) 
    call_command('loaddata', *fixtures, verbosity=1) 
    initialized = True 

    return _app(e,sr) 

res = WSGIResource(reactor, reactor.getThreadPool(), app) 
factory = Site(res) 
reactor.listenTCP(8888, factory) 

    reactor.run() 

我知道這是一個黑客位的,所以如果你已經一個更好的解決方案,請在此報告。

謝謝。

回答