Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

python - TemplateDoesNotExist at, Source does not exist

I'm building a blog website using django. While trying to list the blogs by categories i get this error as TemplateDoesNotExist.

Here's my directory stucture:

enter image description here

blogs/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

blogs/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

blog/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:

    Traceback (most recent call last):
  File "E:ProIde1 - Django Developer Pathphleebvenvlibsite-packagesdjangocorehandlersexception.py", line 47, in inner
    response = get_response(request)
  File "E:ProIde1 - Django Developer Pathphleebvenvlibsite-packagesdjangocorehandlersase.py", line 204, in _get_response
    response = response.render()
  File "E:ProIde1 - Django Developer Pathphleebvenvlibsite-packagesdjangoemplate
esponse.py", 
line 105, in render
    self.content = self.rendered_content
  File "E:ProIde1 - Django Developer Pathphleebvenvlibsite-packagesdjangoemplate
esponse.py", 
line 81, in rendered_content
    template = self.resolve_template(self.template_name)
  File "E:ProIde1 - Django Developer Pathphleebvenvlibsite-packagesdjangoemplate
esponse.py", 
line 63, in resolve_template
    return select_template(template, using=self.using)
  File "E:ProIde1 - Django Developer Pathphleebvenvlibsite-packagesdjangoemplateloader.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

site-page:

enter image description here

What is this source does not exist and why Loader is looking for blog_list.html which doesn't exist?

Also how to fix this error?

question from:https://stackoverflow.com/questions/65899321/templatedoesnotexist-at-source-does-not-exist

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As quoted in comments:

typo your directory is named catergories and your template_name is categories/category_detail.html – Abdul Aziz Barkat


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...