TemplateDoesNotExist位于/​​ home.html

时间:2019-01-07 03:05:36

标签: django-templates

我正在尝试将模板渲染到我的模板文件夹下。我相信我可以正确设置视图,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.

1 个答案:

答案 0 :(得分:0)

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