Shahin Mamedov
Shahin Mamedov

Reputation: 53

TemplateDoesNotExit post_list.html

I have stumbled upon a problem where it tries to look for a template I did not even indented it to. My html files name is home.html but it looks for post_list.html. Here are the few of the files:

urls.py:

from django.urls import path
from . import views
from .views import HomeView

urlpatterns = [
    #path('', views.home, name = "home")
    path('', HomeView.as_view(), name = "home"),
]

views.py

from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Post

# Create your views here.
#def home(request):
#   return render(request, 'home.html', {}) 

class HomeView(ListView):
    model = Post
    template_score = 'home.html' 
]

Here is the directory. enter image description here

The commented lines are the ones that works, but when I try the class HomeView it gives me error.

Exception Type: TemplateDoesNotExist
Exception Value:    
myblog/post_list.html

Upvotes: 1

Views: 225

Answers (2)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476493

The roots that Django searches are the app_name/templates. Since the path is myblog/post_list.html, you need to put a post_list.html template under:

.
└── myblog
    └── templates
        └── myblog
            ├── index.html
            └── post_list.html

You can specify another name with:

class HomeView(ListView):
    model = Post
    template_name = 'myblog/home.html'

Upvotes: 2

Johan
Johan

Reputation: 3611

Your template file should be stored in a subfolder named accordingly to your app, like this

templates/myblog/post_list.html

Upvotes: 1

Related Questions