django-模板在上不存在/

django-模板在上不存在/,django,templates,django-templates,Django,Templates,Django Templates,我正在启动一个新的Django项目,但我无法立即设置正确的模板目录路径。 这是我的设置.py: import os TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates') TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [TEMPLATE_DIR], 'APP_DIRS':

我正在启动一个新的Django项目,但我无法立即设置正确的模板目录路径。 这是我的设置.py:

import os
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                ...
            ],
        },
    },
]
我在项目文件夹中有一个非常简单的视图:

def dashboard(request):

    context = {

    }
    template_name = 'dashboard.html'
    return render(requst, template_name, context)
dahsboard.html位于BASE_DIR/templates/dashboard.html中

非常直截了当,但我还是得到了错误: “Template不做特克斯主义者/ dashboard.html“

这是完整的回溯:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/

Django Version: 3.2
Python Version: 3.8.5
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'Presents',
 'Customers',
 'Orders',
 'Users',
 'comuni_italiani',
 'import_export',
 'crispy_forms']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Template loader postmortem
Django tried loading these templates, in this order:

Using engine django:
    * django.template.loaders.filesystem.Loader: /home/carloc/Projects/natale/src/templates/dashboard.html (Source does not exist)
    * django.template.loaders.app_directories.Loader: /home/carloc/Projects/natale/venv/lib/python3.8/site-packages/django/contrib/admin/templates/dashboard.html (Source does not exist)
    * django.template.loaders.app_directories.Loader: /home/carloc/Projects/natale/venv/lib/python3.8/site-packages/django/contrib/auth/templates/dashboard.html (Source does not exist)
    * django.template.loaders.app_directories.Loader: /home/carloc/Projects/natale/src/Presents/templates/dashboard.html (Source does not exist)
    * django.template.loaders.app_directories.Loader: /home/carloc/Projects/natale/src/Customers/templates/dashboard.html (Source does not exist)
    * django.template.loaders.app_directories.Loader: /home/carloc/Projects/natale/src/Orders/templates/dashboard.html (Source does not exist)
    * django.template.loaders.app_directories.Loader: /home/carloc/Projects/natale/src/Users/templates/dashboard.html (Source does not exist)
    * django.template.loaders.app_directories.Loader: /home/carloc/Projects/natale/venv/lib/python3.8/site-packages/import_export/templates/dashboard.html (Source does not exist)
    * django.template.loaders.app_directories.Loader: /home/carloc/Projects/natale/venv/lib/python3.8/site-packages/crispy_forms/templates/dashboard.html (Source does not exist)



Traceback (most recent call last):
  File "/home/carloc/Projects/natale/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/carloc/Projects/natale/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/carloc/Projects/natale/src/natale/views.py", line 12, in dashboard
    return render(request, template_name, context)
  File "/home/carloc/Projects/natale/venv/lib/python3.8/site-packages/django/shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/home/carloc/Projects/natale/venv/lib/python3.8/site-packages/django/template/loader.py", line 61, in render_to_string
    template = get_template(template_name, using=using)
  File "/home/carloc/Projects/natale/venv/lib/python3.8/site-packages/django/template/loader.py", line 19, in get_template
    raise TemplateDoesNotExist(template_name, chain=chain)

Exception Type: TemplateDoesNotExist at /
Exception Value: dashboard.html

它必须是:
返回呈现(请求、模板\u名称、上下文)
。否则,您能显示完整的回溯吗?对不起,我在从编辑器复制时丢失了那部分代码。我也用trackback更新了这篇文章。谢谢,您在以下方面出现类型错误:return render(request…),它应该是return render(request…)。您可以显示您的项目结构吗?@CarloCogni,正如您所看到的(它不存在):
/home/carloc/Projects/natale/src/templates/dashboard.html(源代码不存在)
。将模板移动到此目录
/home/carloc/Projects/natale/src/templates/