2015-01-24 6 views
1

파일을 출력하기 위해 Python 스크립트에서 장고 템플릿 시스템을 도구로 사용하려고합니다. 장고 프레임 워크의 다른 구성 요소는 필요하지 않습니다. Django 템플릿 시스템 독립 실행 형

나는 configure the template system for standalone mode 지침에 따라,하지만 난 template.loader.render_to_string(), template.loader.get_template(), 또는 template.render(Content())을 사용하려고하면 나는 AppRegistryNotReady 예외 돌아가 다음 문서에 설명 된대로 나는 다음과 같은 settings.configure() 호출이

raise AppRegistryNotReady("Apps aren't loaded yet.") 
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. 

을 :

settings.configure(
    DEBUG=DEBUG, 
    TEMPLATE_DEBUG=True, 
    TEMPLATE_DIRS=(
     POSTS_DIR, 
     TEMPLATES_DIR, 
     PAGES_DIR, 
    ), 
) 

INSTALLED_APPS=()도 마찬가지입니다. 동일한 문제가 있습니다. 나는 단지 템플릿 시스템을 사용하고 싶다. 템플릿 로더를 제외하고는 아무 것도 필요하지 않으므로 extend 템플릿을 사용할 수 있습니다.

답변

2

이 오류는 django.template.loaders.app_directories.Loader에서 발생했다고 생각됩니다. TEMPLATE_LOADERS 설정을 추가하려고 : 1.10 장고에서 여기에 온 사람들을 위해

settings.configure(
    DEBUG=DEBUG, 
    TEMPLATE_DEBUG=True, 
    TEMPLATE_DIRS=(
     POSTS_DIR, 
     TEMPLATES_DIR, 
     PAGES_DIR, 
    ), 
    TEMPLATE_LOADERS=('django.template.loaders.filesystem.Loader',), 
) 
+0

덕분에, 나는'django.template.loaders.app_directories.Loader'은 기본적으로 활성화 된 것을 몰랐다. 'filesystem.loader'를 지정하면 트릭이 생겼습니다! – austinheiman

2

, 그리고 나처럼, 유닛 테스트와 장고 템플릿 언어를 테스트 할, 유 엔진 클래스를 사용해야합니다.

[email protected]:~/src$ python3 
Python 3.5.2 (default, Sep 10 2016, 08:21:44) 
[GCC 5.4.0 20160609] on linux 
Type "help", "copyright", "credits" or "license" for more information. 
.>>> from django.template import Engine, Context 
.>>> _template = Engine().from_string('my name {{ name }}') 
.>>> _context = Context({'name':'Claudio Santos'}) 
.>>> _template.render(_context) 
'my name Claudio Santos' 

참조 문헌 :

Upgrading Django 1.8

The Engine Class

Using Settings without DJANGO_SETTINGS_MODULE