2017-09-23 61 views
-1

我與Django的獨立腳本嘗試,當我離開調用開發服務器命令行調用的東西從https://github.com/syntarsus/minimal-django試圖重寫AppRegistryNotReady在獨立的Django

繼承順利:

python minimal.py runserver 

的服務器是高興起來並運行:

Performing system checks... 

System check identified no issues (0 silenced). 
September 24, 2017 - 00:58:36 
Django version 1.11.5, using settings None 
Starting development server at http://127.0.0.1:8000/ 
Quit the server with CTRL-BREAK. 

我想修改腳本去一個水平下降在Django的抽象和調用服務器:

from django.core import management  
management.call_command('runserver') 

,這將引發異常

AppRegistryNotReady: Apps aren't loaded yet. 

正如在其他SO問題的建議,我試圖重寫的部份有例外:

import django 
django.setup() 

...但顯然它不工作再一次,所以我迷路了。

回答

1

這爲我工作,通過簡單地做python minimal.py調用,它會運行一個本地服務器:

minimal.py

import sys 

import django 
from django.conf import settings 
from django.conf.urls import url 
from django.core.management import call_command 
from django.http import HttpResponse 

settings.configure(
    DEBUG=True, 
    SECRET_KEY='A-random-secret-key!', 
    ROOT_URLCONF=sys.modules[__name__], 
) 
django.setup() 


def index(request): 
    return HttpResponse('<h1>A minimal Django response!</h1>') 

urlpatterns = [ 
    url(r'^$', index), 
] 

if __name__ == '__main__': 
    call_command('runserver') 
+0

工作對我來說,我只是想提更好地利用commnad在IDE上運行腳本(Spyder)只是掛在它上面。 –