简体   繁体   English

Django:TemplateDoesNotExist错误

[英]Django: TemplateDoesNotExist Error

I'm working on linking a template to a view but I'm having this problem. 我正在将模板链接到视图,但遇到了这个问题。

I do not know where the problem is 我不知道问题出在哪里

This is the traceback 这是回溯

 Traceback: File "/home/choco/.local/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner 35. response = get_response(request) File "/home/choco/.local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response 128. response = self.process_exception_by_middleware(e, request) File "/home/choco/.local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response 126. response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/choco/python3/django/choco/blog/views.py" in post_list 4. return render(request, 'blog/post_list.html', {}) File "/home/choco/.local/lib/python3.6/site-packages/django/shortcuts.py" in render 36. content = loader.render_to_string(template_name, context, request, using=using) File "/home/choco/.local/lib/python3.6/site-packages/django/template/loader.py" in render_to_string 61. template = get_template(template_name, using=using) File "/home/choco/.local/lib/python3.6/site-packages/django/template/loader.py" in get_template 19. raise TemplateDoesNotExist(template_name, chain=chain) Exception Type: TemplateDoesNotExist at / Exception Value: blog/post_list.html 

blog/blog/urls.py 博客/博客/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.post_list, name = 'post_list'),
]

blog/mysite/urls.py 博客/mysite/urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('blog.urls')),
]

blog/blog/views.py 博客/博客/views.py

from django.shortcuts import render

def post_list(request):
    return render(request, 'blog/post_list.html', {})

blog/blog/blog/templates/blog/post_list.html 博客/博客/博客/模板/博客/post_list.html

blog/mysite/settings.py 博客/mysite/settings.py

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

Check your project directory's hierarchy. 检查项目目录的层次结构。 It should be something like: 应该是这样的:

myproject
-> mysite
  -> mysite
    - __init__.py
    - settings.py
  -> blog
    -> templates
    - urls.py
    - views.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM