python - TemplateDoesNotExist 在,源不存在
问题描述
我正在使用 django 构建一个博客网站。在尝试按类别列出博客时,我收到此错误为TemplateDoesNotExist。
这是我的目录结构:
博客/models.py:
class Category(models.Model):
title = models.CharField(max_length=50)
slug = models.SlugField(editable=False)
def save(self, *args, **kwargs):
self.slug = f'{slugify(self.title)}--{uuid.uuid4()}'
super(Category, self).save(*args, **kwargs)
def __str__(self):
return self.title
def blog_count(self):
return self.blogs.all().count()
class Blog(models.Model):
title = models.CharField(max_length=150)
content = models.TextField()
publishing_date = models.DateTimeField(auto_now_add=True)
image = models.ImageField(upload_to='uploads/', blank=True, null=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
slug = models.SlugField(editable=False)
category = models.ForeignKey(Category, on_delete=models.CASCADE, blank=True, related_name='blogs')
def save(self, *args, **kwargs):
self.slug = f'{slugify(self.title)}--{uuid.uuid4()}'
super(Blog, self).save(*args, **kwargs)
def __str__(self):
return self.title
博客/views.py:
class CategoryDetail(ListView):
model = Blog
template_name = 'categories/category_detail.html'
context_object_name = 'blogs'
def get_queryset(self):
self.category = get_object_or_404(Category, pk=self.kwargs['pk'])
return Blog.objects.filter(category=self.category).order_by('-id')
def get_context_data(self, **kwargs):
context = super(CategoryDetail, self).get_context_data(**kwargs)
return context
博客/urls.py:
app_name = 'blogs'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('detail/<int:pk>/<slug:slug>', views.BlogDetail.as_view(), name='detail'),
path('category/<int:pk>/<slug:slug>', views.CategoryDetail.as_view(), name='category_detail'),
]
phleeb/setting.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [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',
],
},
},
]
追溯:
Traceback (most recent call last):
File "E:\ProIde\1 - Django Developer Path\phleeb\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "E:\ProIde\1 - Django Developer Path\phleeb\venv\lib\site-packages\django\core\handlers\base.py", line 204, in _get_response
response = response.render()
File "E:\ProIde\1 - Django Developer Path\phleeb\venv\lib\site-packages\django\template\response.py",
line 105, in render
self.content = self.rendered_content
File "E:\ProIde\1 - Django Developer Path\phleeb\venv\lib\site-packages\django\template\response.py",
line 81, in rendered_content
template = self.resolve_template(self.template_name)
File "E:\ProIde\1 - Django Developer Path\phleeb\venv\lib\site-packages\django\template\response.py",
line 63, in resolve_template
return select_template(template, using=self.using)
File "E:\ProIde\1 - Django Developer Path\phleeb\venv\lib\site-packages\django\template\loader.py", line 47, in select_template
raise TemplateDoesNotExist(', '.join(template_name_list), chain=chain)
django.template.exceptions.TemplateDoesNotExist: categories/category_detail.html, blogs/blog_list.html
[26/Jan/2021 15:33:21] "GET /category/1/django--b3a4b54f-6f0e-40e9-9b6b-e87dc502d31a HTTP/1.1" 500 82719
网站页面:
什么是不存在的源以及为什么 Loader 正在寻找不存在的 blog_list.html?
还有如何解决这个错误?
解决方案
正如评论中所引用的:
拼写错误,您的目录名为categories,模板名称为category/category_detail.html – Abdul Aziz Barkat
推荐阅读
- elasticsearch - 如何安排这个logstash配置文件每1分钟运行一次?
- .net-core - 如何验证在 jwt.io 上使用 Keycloak 身份验证提供程序创建的 HS256 签名 JWT 令牌
- java - Java 转 ASN1 可读字符串
- php - 在 laravel 中下载之前显示或预览 pdf 文件
- python - 在发布期间/之前清理 Git 历史记录、添加新提交并将更改带回开发人员的推荐方法
- c++ - 如何从圆上的中间点和其他点之间的角度计算圆上的点
- java - Git - 在不同的行上创建一个带有键值对和符号的 JSON 文本文件,以确保没有合并问题
- python - 将行转换为熊猫列下的值列表
- cassandra - 尝试在 cqlsh 中运行创建和选择时出错:NoHostAvailable
- php - builder where 和 codeigniter query builder 工作方式的区别