Python Django;TemplateDoesNotExist错误-博客应用程序
我试图创建一个博客应用程序后,奥琳教程,但与加载一些模板挣扎 似乎blog/templates/blog/中的模板。。。找不到 页面加载良好。下面是settings.py、views.py和url.py文件中的当前代码 设置.pyPython Django;TemplateDoesNotExist错误-博客应用程序,python,django,python-3.x,django-templates,django-views,Python,Django,Python 3.x,Django Templates,Django Views,我试图创建一个博客应用程序后,奥琳教程,但与加载一些模板挣扎 似乎blog/templates/blog/中的模板。。。找不到 页面加载良好。下面是settings.py、views.py和url.py文件中的当前代码 设置.py BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ... INSTALLED_APPS = [ 'django.contrib.admin', 'djan
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)
url.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),
]
从django.url导入路径
从。视图导入(
blog\u post\u create\u view,
博客(博文)(详情)(视图),,
博客发布列表视图,
)
应用程序名称='博客'
URL模式=[
路径(“/”,博客文章详细信息视图),
路径(“”,博客文章列表视图),
路径('new-post',blog\u post\u create\u view),
]
模板的路径不同(缺少“blog/”):
您还需要保证,您已经在此处制作了模板:
blog/templates/blog/blog_post_list.html <...>
blog/templates/blog/blog\u post\u list.html
谢谢你的回答!没有意识到不更新其他视图会导致错误。。
blog/templates/blog/blog_post_list.html <...>