Hi folks,
I’m new to django and following this website (Chapter 4: Message Board App | Django For Beginners)
Structure of my file is like :
main_prj,SQL,app_name,manage.py
app_name → other files… ,(templates → app_name-> HTML file)
I’ve used ListView template in views.py & error is coming
Note: When posting code here, do not post images of code. Copy/paste the code into your reply, surrounded by lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```.
First, the reference to post_list.html is a reference to the default view name for a ViewList. If it can’t find the view named in the template_name attribute of the class, it will try to find that default view. So the root cause here is that it’s not finding your home.html template.
Now, note that the tutorial has:
In your text editor, create a new file called templates/home.html
So the tutorial is written to have you put your template directly in the templates directory, not within a subdirectory for the app.
You can either change the location of your home.html template or change your reference in the template_name attribute to match its actual location.
The TEMPLATES DIRS setting is already relative to your project root, it’s not an absolute path. You should not prepend BASE_DIR on to the DIRS entries.
(Or to be more precise, it’s generally defined that way. It can be absolute, but then in this case, you’d want to verify that the entry being created is accurate.)
Side note: Additionally, if you’re going to store and use templates in apps, you may also want to set the APP_DIRS setting to true. In the “common case”, DIRS is set to a project-level templates directory while the APP_DIRS setting is used to specify that templates could exist in the individual app_dir/templates directory.
At this point then, my recommendation for you would be to follow the tutorial exactly. Make all settings and file locations the same as what the tutorial defines. Start with something known to be correct, then if you wish to change it, make those changes.
from django.urls import path
from .views import HomePage
urlpatterns = [
path('',HomePage.as_view(),name = 'home'),
]
post [views.py]
from django.shortcuts import render
from django.views.generic import ListView
from .models import Post
# Create your views here.
class HomePage(ListView):
model = Post
template = "home.html"