【问题标题】:TemplateDoesNotExist at / home.htmlTemplateDoesNotExist at / home.html
【发布时间】:2019-06-01 17:55:54
【问题描述】:

我正在尝试在我的模板文件夹下渲染一个模板。我相信我正确设置了我的视图、URL 设置和渲染视图的设置。但是,我不断收到相同的错误,找不到模板。我一直认为这是一个路径问题,但无法解决这个问题。我添加了我的代码的不同视图的屏幕。

# In newword/newword/newword/views.py

from django.http import HttpResponse
from django.shortcuts import render

def home(request):

  return render(request, 'home.html', {})


# In newword/newword/newword/settings.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [   'templates/home.html'],
        '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',
            ],
        },
    },
]

# In newword/newword/newword/urls.py

from django.contrib import admin
from django.urls import path
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home),`enter code here`

]


# In newword/newword/newword/templates/home.html


<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>Home</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" type="text/css" media="screen" href="main.css" />
    <script src="main.js"></script>
  </head>
  <body></body>
</html>



# Error Output

TemplateDoesNotExist at /
home.html
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 2.1.5
Exception Type: TemplateDoesNotExist
Exception Value:    
home.html
Exception Location: /usr/local/lib/python3.7/site-packages/django/template/loader.py in get_template, line 19
Python Executable:  /usr/local/opt/python/bin/python3.7
Python Version: 3.7.2
Python Path:    
['/Users/robertromulus/pythonProjects/newword/newword',
 '/usr/local/Cellar/python/3.7.2/Frameworks/Python.framework/Versions/3.7/lib/python37.zip',
 '/usr/local/Cellar/python/3.7.2/Frameworks/Python.framework/Versions/3.7/lib/python3.7',
 '/usr/local/Cellar/python/3.7.2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload',
 '/Users/robertromulus/Library/Python/3.7/lib/python/site-packages',
 '/usr/local/lib/python3.7/site-packages']
Server time:    Mon, 7 Jan 2019 03:02:48 +0000
Template-loader postmortem

Django tried loading these templates, in this order:

Using engine django:
django.template.loaders.filesystem.Loader: /Users/robertromulus/pythonProjects/newword/newword/templates/home.html/home.html (Source does not exist)
django.template.loaders.app_directories.Loader: /usr/local/lib/python3.7/site-packages/django/contrib/admin/templates/home.html (Source does not exist)
django.template.loaders.app_directories.Loader: /usr/local/lib/python3.7/site-packages/django/contrib/auth/templates/home.html (Source does not exist)
Traceback Switch to copy-and-paste view

/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py in inner
            response = get_response(request) ...
▶ Local vars
/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py in _get_response
                response = self.process_exception_by_middleware(e, request) ...
▶ Local vars
/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py in _get_response
                response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/Users/robertromulus/pythonProjects/newword/newword/newword/views.py in home
     return render(request, 'home.html', {}) ...
▶ Local vars
/usr/local/lib/python3.7/site-packages/django/shortcuts.py in render
    content = loader.render_to_string(template_name, context, request, using=using) 


I want the home.html to be rendered when I go to the root path of the site.

【问题讨论】:

    标签: django-templates


    【解决方案1】:

    问题已解决 - 我需要添加另一个文件夹,然后添加模板,然后在模板下添加每个 html 文件。这个视频有帮助。 https://www.youtube.com/watch?v=xp_N4JnxHIk&t=184s&list=PLhTjy8cBISEpXc-yjjSW90NgNyPYe7c9_&index=6

    【讨论】:

      猜你喜欢
      • 2022-01-11
      • 1970-01-01
      • 2023-03-27
      • 2021-10-12
      • 2013-07-29
      • 1970-01-01
      • 1970-01-01
      • 2016-08-27
      • 2020-06-20
      相关资源
      最近更新 更多