templates配置
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'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.template的loader获取template:
from django.template import loader
def index(request):
template = loader.get_template("music/index.html")
context = {
'all_albums':all_albums,
}
return HttpResponse(template.render(context,request))
shortcuts渲染模板
from django.shortcuts import render
def index(request):
all_albums = Album.objects.all()
context = {
"all_albums":all_albums,
}
return render(request, "music/index.html", context)
网友评论