如何修复pycharm无法识别模板文件夹

2024-09-30 22:28:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为“homepage.html”的html模板,我的项目设置如下:

但是,在运行时,我会遇到以下错误:

django.template.loaders.filesystem.Loader: D:\lirdi\python\django_blog_project\djangonautic\home\homepage.html (Source does not exist)
django.template.loaders.app_directories.Loader: D:\lirdi\python\django_blog_project\interpreter\lib\site-packages\django\contrib\admin\templates\home\homepage.html (Source does not exist)
django.template.loaders.app_directories.Loader: D:\lirdi\python\django_blog_project\interpreter\lib\site-packages\django\contrib\auth\templates\home\homepage.html (Source does not exist)

以下是我的模板代码:

<html>
   <head>
       <title>Homepage</title>
   </head>
   <body>
   <h1>This is the homepage</h1>
   <p>Welcome to Djangonautic</p>
   </body>
</html>

在我看来:

def homepage(reqeust):
    return render(reqeust,'home/homepage.html')

在我的设置文件中:

'DIRS': ['templates'],

Tags: djangoprojectsourcehomehtmlnottemplateblog
1条回答
网友
1楼 · 发布于 2024-09-30 22:28:35

模板home/homepage.html在您指定的位置不存在(我看不到名为“home”的文件夹)。理想情况下,templates文件夹应该位于更高级别的目录中,直接位于第一个djangonautic文件夹下

尝试将templates文件夹移动到django_blog_project/djangonautic/templates,只需使用:

def homepage(request):
   return render(request,'homepage.html')

确保在settings.py文件中正确指定了主模板目录,例如:

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            # ... some options here ...
        },
    },
]

相关问题 更多 >