2017-06-23 4 views
1
from django.http import HttpResponse 
from django.template import loader 
from .models import Album 

def index(request): 
    all_albums=Album.objects.all() 
    template= loader.get_template('music/index.html') 
    context= { 
     'all_albums': all_albums, 
    } 
    return HttpResponse(template.render(context,request)) 


def detail(request, album_id): 
    return HttpResponse("<h2>details for album id " + str(album_id) + " 
</h2>") 
+0

新しいIAMハイテクはジャンゴすると、この問題に直面して コードの一部が です。有効なパスがまだ見えていないにもかかわらず、loader.get_template()でパスに関する問題が発生しています。 –

+1

テンプレートはどこにありますか? 'templates/music/index.html'にありますか? – zaidfazil

+0

あなたのindex.htmlが存在するパスを確認するより良い方法は、あなたのsettings.pyファイルのパスをtemplate_pathに設定することです – ammy

答えて

0

デフォルトでDjangoはすべてのアプリケーションディレクトリのサブディレクトリtemplatesのテンプレートをチェックします。したがって、テンプレートを各アプリケーションのtemplatesサブディレクトリの下に置くことができます。

project 
--app 
    --templates 
    --music 
     --index.html 

それとも、あなたはDjangoはsettings.pyでTEMPLATE設定でDIRSオプションでテンプレートを探す必要があるディレクトリを指定することができ、

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': ['here-specify-the-path', 'as-strings'], 
     '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', 
      ], 
     },  
    }, 
]