首先,你应该在 settings.py 中配置你的模板静态路径,类似于下面这样:
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.context_processors.i18n',
],
},
},
]
APP_DIRS=True
的意思是,Django 会在每个应用程序目录中查找模板,在你的例子中是 main_app/
和 current_app/
:
你只需要提到模板路径,并将其视为根路径,如下所示:
def temp_view(request):
....
return render(request, "pages/home.html",context)
Django将在
main_app/
目录下查找文件
pages/home.html
。
pages/home.html
的问题。 - Sayse"pages/home.html"
,Django 会自动处理它。 - Rohit Jain