【问题标题】:How to fix pycharm not recognising templates folder如何修复pycharm无法识别模板文件夹
【发布时间】:2020-10-12 19:04:14
【问题描述】:

我有一个名为“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'],

【问题讨论】:

    标签: python django web django-templates


    【解决方案1】:

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

    尝试将模板文件夹移动到 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 ...
            },
        },
    ]
    

    【讨论】:

    • 但我尝试在 djangonautic 文件夹下更改模板文件夹,它工作了谢谢
    猜你喜欢
    • 2021-11-05
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 2022-08-08
    相关资源
    最近更新 更多