【问题标题】:Django; TemplateDoesNotExist error - blog app姜戈; TemplateDoesNotExist 错误 - 博客应用程序
【发布时间】:2019-09-23 21:46:47
【问题描述】:

我正在尝试按照在线教程创建博客应用程序,但在加载一些模板时遇到了困难。

好像找不到 blog/templates/blog/... 中的模板。

页面http://127.0.0.1:8000/blog/ 加载正常。下面包括来自 settings.py、views.py 和 urls.py 文件的当前代码。

settings.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

... 

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # third party installs go here.
    'products',
    'pages',
    'blog',
]

...

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

views.py

from django.shortcuts import render, get_object_or_404
from django.http import Http404

# Create your views here.

from .models import BlogPost
def blog_post_list_view(request):
    qs = BlogPost.objects.all()  # queryset -> list of python objects
    template_name = 'blog/blog_post_list.html'
    context = {"object_list": qs}
    return render(request, template_name, context)


def blog_post_create_view(request):
    # create objects
    #template_name = 'blog/blog_post_create.html'
    context = {"form": None}
    return render(request, "blog/blog_post_create.html", context)


def blog_post_detail_view(request, slug):
    obj = get_object_or_404(BlogPost, slug=slug)
    template_name = 'blog_post_detail.html'
    context = {"object": obj}
    return render(request, template_name, context)


def blog_post_update_view(request, slug):
    obj = get_object_or_404(BlogPost, slug=slug)
    template_name = 'blog_post_update.html'
    context = {"object": obj, 'form': None}
    return render(request, template_name, context)


def blog_post_delete_view(request, slug):
    obj = get_object_or_404(BlogPost, slug=slug)
    template_name = 'blog_post_delete.html'
    context = {"object": obj}
    return render(request, template_name, context)

urls.py

from django.urls import path
from .views import (
    blog_post_create_view,
    blog_post_detail_view,
    blog_post_list_view,
)

app_name = 'blog'
urlpatterns = [
    path('<str:slug>/', blog_post_detail_view),
    path('', blog_post_list_view),
    path('new-post', blog_post_create_view),
]

【问题讨论】:

    标签: python django python-3.x django-templates django-views


    【解决方案1】:

    您的模板路径不同(缺少“blog/”):

    template_name = 'blog/blog_post_list.html'
    template_name = 'blog_post_detail.html'
    template_name = 'blog_post_update.html'
    

    您还需要确保您已在此处制作模板:

    blog/templates/blog/blog_post_list.html <...>
    

    【讨论】:

    • 感谢您的回答!没有意识到不更新其他视图会导致错误..
    猜你喜欢
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2016-02-02
    • 2010-10-12
    • 2016-05-07
    • 2020-08-17
    • 2018-02-01
    • 2018-02-11
    相关资源
    最近更新 更多