Django:找不到模板/index.html

时间:2019-10-22 11:15:31

标签: python django

我正在使用Django运行网站。登录没有问题。当我登录并单击某些仪表板时,它显示“找不到页面”(404)。

Views.py:

With Sheets("TEST").Range("num")
    .Copy
    .Offset(, .Columns.Count).Insert xlShiftToRight
End With

Settings.py

def index(request):
return(request,'obs_app/index.html')

urls.py:

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates/index.html')
TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [TEMPLATE_DIR,],
    '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',
            ],
        'libraries':  {
            'get_by_index':'obs_app.templatetags.templatefilters',
            'get_by_key':'obs_app.templatetags.templatefilters',
            'get_dict':'obs_app.templatetags.templatefilters',
            'get_items':'obs_app.templatetags.templatefilters',
            'multiple':'obs_app.templatetags.templatefilters',
        },
    },
},
]

我遇到的错误是: 找不到页面(404) 当前路径index.html与任何这些都不匹配。

2 个答案:

答案 0 :(得分:1)

您必须渲染模板并修复缩进

def index(request):
   return render(request,'obs_app/index.html')

在这里,您还需要提供目录而不是文件

TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')

答案 1 :(得分:0)

更改:

TEMPLATE_DIR = os.path.join(BASE_DIR,'templates/index.html')

收件人:

TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')

TEMPLATES = [
    {
        ...
        'DIRS': [],    # DIRS defines a list of directories where the engine should look for template source files, in search order.
        ...
    },
]

您可能需要阅读:Support for template engines

相关部分复制到此处:

  

DIRS定义了目录列表,引擎应在目录中按搜索顺序查找模板源文件。