首页 > 解决方案 > 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?

还有如何解决这个错误?

标签: pythonpython-3.xdjangodjango-templates

解决方案


正如评论中所引用的:

拼写错误,您的目录名为categories,模板名称为category/category_detail.html – Abdul Aziz Barkat


推荐阅读