Home > Net >  How to fix this error: Template Does Not Exist ? Django
How to fix this error: Template Does Not Exist ? Django

Time:10-05

urls.py

from django.urls import path

from . import views

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

another urls.py

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

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

settings.py - templates

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'tryingDjango/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',
            ],
        },
    },
    
    

views.py

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

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

I am stuck here, tried changing many already, still won't work, the 500(Template does not exist), what is the right code?

Thanks in advance!

CodePudding user response:

In your settings.py file:

inside TEMPLATES, just add this:

[os.path.join(BASE_DIR, 'templates')]

Ex:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')], #Added here
        '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',
            ]
        },
    },
]
  • Related