【问题标题】:Why can't you find the template?为什么找不到模板?
【发布时间】:2023-01-14 00:58:56
【问题描述】:

这是设置.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',
            ],
        },
    },
]

这是我的观点.py

def iniciar_sesion(request):
    return render(request,'registrar/iniciar_sesion.html')

这是结构

appweb
|-plantillas
   |-registrar
     |-iniciar_sesion.html

奇怪,他找不到,我不知道他为什么找不到

Structure with image

【问题讨论】:

  • 这回答了你的问题了吗? Django Can't Find My Templates
  • @AminSamani 不,请现在看问题因为我编辑
  • 您是否尝试过将plantillas 重命名为templates
  • @Jacinator 看照片

标签: python django


【解决方案1】:

您对模板目录的设置是 os.path.join(BASE_DIR,"templates"),但您的文件夹结构显示为 plantillas/registrar/iniciar_session.html。

在 DIRS 中将模板更改为 plantillas:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,"plantillas")],
        '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 找不到您的模板时,使用模板加载器事后分析,这是黄色栏下的第一个灰色部分,上面写着 TemplateDoesNotExist at /。它会告诉您它试图查看的位置,您可以将其与应该查看的位置进行比较。

希望这可以帮助!

【讨论】:

    猜你喜欢
    • 2019-11-22
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2021-09-18
    相关资源
    最近更新 更多