Skip to content
Advertisement

Template file not found Django

I am trying to make a home page of a new website via Django. My app name is ‘blog’, home page is home.html I still receive the error template does not exist when I go to http://127.0.0.1:8000/blog/home/

I made sure I added ‘blog’ to my templates in settings.py and that I added the folder templates in the main directory as well as through blog/templates/blog/home.html

myproject/blog/views.py

from django.shortcuts import render
from django.http import HttpResponse


def home(request):
    return render(request, 'blog/home.html')

myproject/blog/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('home/', views.home, name='home'),

]

myproject/settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(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',
            ],

myproject/urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include('blog.urls')),
]

Do you see anything in my code which causes a problem? I receive the message in blog/views.py that “Template file ‘blog’ not found” on the line

return render(request, ‘blog/home.html’)

Advertisement

Answer

You are doing it wrong. You need to read the Django documentation carefully and try to understand whatever you read and implement the same step by step. The url you have to hit is

http://127.0.0.1:8000/blog/home/

home.html will be rendered at this url. You don’t put html page name in the url

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement