Django 2.0.2版中的TemplateDoesNotExist错误

时间:2018-03-31 09:18:56

标签: django django-templates python-3.5

My Django project structure

代码是:

settings.py是:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        # 'DIRS': ['templates', ],
        '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',
            ],
        },
    },
]


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'python_django_blog',
        'USER': 'root',
        'PASSWORD': 'Coder123',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}

urls.py是:

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

from first import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('post-list/', views.postlist, name='post-list'),
]

views.py是:

from django.shortcuts import render
from django.template.loader import render_to_string

from .models import Post

def postlist(request):
    all_post=Post.objects.all()
    # return render(request,'post_list.html', all_post)
    return render_to_string(request,'post_list.html', {'all_post_list': all_post})

post_list.html是:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Oh No</title>
</head>
<body>
    <h1>Problem and problem</h1>
</body>
</html>

Problem is

我认为,所有设置都没问题。 http://127.0.0.1:8000/admin/也可以。但是我输入错误http://127.0.0.1:8000/post-list/。我搜索了许多类似的问题。修好后,我已经制作了上述代码。但这些代码显示错误。我无法弄清楚问题是什么。帮我解决这个问题。

提前致谢。

1 个答案:

答案 0 :(得分:0)

试试这个 - 'DIRS':[os.path.join(BASE_DIR,'first / templates')],