【问题标题】:TemplateDoesNotExist error in Django version 2.0.2Django 2.0.2 版中的 TemplateDoesNotExist 错误
【发布时间】:2018-09-10 04:33:22
【问题描述】:

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/ 时遇到错误。我已经搜索了许多类似的问题。修复后,我已经制作了上述代码。但是这些代码显示错误。我无法弄清楚问题是什么。帮我解决这个问题。

提前致谢。

【问题讨论】:

    标签: django django-templates python-3.5


    【解决方案1】:

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

    【讨论】:

    • 在views.postlist 方法中,我用render 替换了render_to_string 方法。现在可以了。
    猜你喜欢
    • 2018-07-17
    • 1970-01-01
    • 2014-02-19
    • 2013-06-20
    • 2015-09-02
    • 2012-12-18
    • 1970-01-01
    • 2023-03-07
    • 2014-05-27
    相关资源
    最近更新 更多