2017-08-10 4 views
3

settings.pyのTEMPLATESにある 'DIRS'はなぜ 'APP_DIRS'がFalseに設定されている場合にのみ効果がありますか?なぜ 'DIRS'はAPP_DIRSがFalseのときだけ効果がありますか?

カスタムHTMLウィジェットを読み込もうとしましたが、 'DIRS'を変更しても 'TemplateDoesNotExist'がスローされたときに 'Template-loader postmortem'は変更されませんでした。 「APP_DIRS」をFalseに設定すると、「DIRS」設定が突然効果を上げました。

私は同様の質問を検索しようとしましたが、回答が見つかりませんでした。私はまた、ドキュメントを見てきましたが、DIRSやAPP_DIRSについての段落でも、他のものがそうでない場合には、どちらかが動作するとは言いません。

例1:

TEMPLATES = [ 
{ 
    'BACKEND': 'django.template.backends.django.DjangoTemplates', 
    'DIRS': [os.path.join(BASE_DIR, 'templates'), '/Users/jonas/Documents/jobb/dynamicSurvey/survey/templates/django/forms/widgets'], 
    'APP_DIRS': True, 
    'OPTIONS': { 
     'context_processors': [ 
      'django.template.context_processors.debug', 
      'django.template.context_processors.request', 
      'django.contrib.auth.context_processors.auth', 
      'django.contrib.messages.context_processors.messages', 
     ], 
    }, 
}, 
] 

この出力を与える:

をエンジンジャンゴを使用:

テンプレートローダは

Djangoはこの順序で、これらのテンプレートをロードする試みを死後

django.template.loaders.filesystem.Loader: /Users/jonas/venv/lib/python3.6/site-packages/django/forms/templates/horizontal_select.html (Source does not exist) 
django.template.loaders.app_directories.Loader: /Users/jonas/venv/lib/python3.6/site-packages/nested_admin/templates/horizontal_select.html (Source does not exist) 
django.template.loaders.app_directories.Loader: /Users/jonas/venv/lib/python3.6/site-packages/django/contrib/admin/templates/horizontal_select.html (Source does not exist) 
django.template.loaders.app_directories.Loader: /Users/jonas/venv/lib/python3.6/site-packages/django/contrib/auth/templates/horizontal_select.html (Source does not exist) 
django.template.loaders.app_directories.Loader: /Users/jonas/Documents/jobb/dynamicSurvey/survey/templates/horizontal_select.html (Source does not exist) 
django.template.loaders.app_directories.Loader: /Users/jonas/venv/lib/python3.6/site-packages/tellme/templates/horizontal_select.html (Source does not exist) 
django.template.loaders.app_directories.Loader: /Users/jonas/venv/lib/python3.6/site-packages/tinymce/templates/horizontal_select.html (Source does not exist) 
django.template.loaders.app_directories.Loader: /Users/jonas/venv/lib/python3.6/site-packages/django/forms/templates/horizontal_select.html (Source does not exist) 

例2:

​​

この出力を与える:

をエンジンジャンゴを使用:

テンプレートローダは

Djangoはこの順序で、これらのテンプレートをロードする試みを死後

django.template.loaders.filesystem.Loader: /Users/jonas/Documents/jobb/dynamicSurvey/templates/survey/survey_detail.html (Source does not exist) 
django.template.loaders.filesystem.Loader: /Users/jonas/Documents/jobb/dynamicSurvey/survey/templates/django/forms/widgets/survey/survey_detail.html (Source does not exist) 

私が「engine django」を正しく理解していれば、例2の最後の行( 'django.template.loaders.filesystem.Loader:/ Users/jonas/Documents/jobb/dynamicSurvey/survey/templates/django/forms/widgets/survey/')は、DIRSの設定が有効であれば、例1で検索する必要があります。

私はスタックに新しく、私の質問を批判することは自由です。

+0

これはどのDjangoバージョンですか? –

+0

Django 1.11.2 – Jonas

答えて

1

FORM_RENDERERの設定をTemplatesSettingレンダラーを使用するように変更します。 TEMPLATESの設定が使用されます。

FORM_RENDERER = 'django.forms.renderers.TemplatesSetting' 

ドキュメントが示唆したように、私は再びTrueから'APP_DIRS'を設定しますと、Djangoはデフォルトのテンプレートを見つけることができるように、あなたのINSTALLED_APPSdjango.formsを追加します。

+0

'django.forms'は既に 'INSTALLED_APPS'に追加されています。私はあなたが言ったようにFORM_RENDERERを設定しましたが、それでも効果はありません。 (これはデフォルトですが、どういう意味ですか?)また、[TemplatesSetting](https://docs.djangoproject.com/en/1.11/)で述べたように、django .__ path __ [0] + '/ forms/templates'の結果を追加しました。 ref/forms/renderers /#templatessetting)を 'DIRS'に変更します。 'APP_DIRS'もTrueに設定されています。 – Jonas

+0

デフォルトは 'DjangoTemplates'です。私は 'TemplatesSetting'を使うことを提案しました(私は誤って' django.forms.renderers.DjangoTemplates'を最初にコピーしましたが)。既に 'APP_DIRS'が' True'に設定され、 'django.forms'が' INSTALLED_APPS'に含まれている場合、 'django .__ path __ [0] + '/ forms/templates''の結果を追加する必要はありません。 – Alasdair

関連する問題