Home > OS >  Can't solve django templatedoesnotexist error
Can't solve django templatedoesnotexist error

Time:10-01

I know this question has been asked many times before yet I either can't find an answer that isn't outdated or that generally works.

I keep getting this error even though my server is running correctly

 Environment:


Request Method: GET
Request URL: http://localhost:8000/subscribe/

Django Version: 3.1.4
Python Version: 3.8.10
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'app',
 'subscribe']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Template loader postmortem
Django tried loading these templates, in this order:

Using engine django:
    * django.template.loaders.app_directories.Loader: /usr/local/lib/python3.8/dist-packages/django/contrib/admin/templates/subscribe/index.html (Source does not exist)
    * django.template.loaders.app_directories.Loader: /usr/local/lib/python3.8/dist-packages/django/contrib/auth/templates/subscribe/index.html (Source does not exist)
    * django.template.loaders.app_directories.Loader: /root/project/subscribe/templates/subscribe/index.html (Source does not exist)



Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/root/project/subscribe/views.py", line 16, in subscribe
    return render(request, 'subscribe/index.html', {'form':sub})
  File "/usr/local/lib/python3.8/dist-packages/django/shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/usr/local/lib/python3.8/dist-packages/django/template/loader.py", line 61, in render_to_string
    template = get_template(template_name, using=using)
  File "/usr/local/lib/python3.8/dist-packages/django/template/loader.py", line 19, in get_template
    raise TemplateDoesNotExist(template_name, chain=chain)

Exception Type: TemplateDoesNotExist at /subscribe/
Exception Value: subscribe/index.html

these are my template settings

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

i've tried changing DIRS and adding a path etc but nothing changes.

i've added all my apps into INSTALLED_APPS.

edit 1: my /subscribe/views.py

from django.shortcuts import render
from project.settings import EMAIL_HOST_USER
from . import forms
from django.core.mail import send_mail

def subscribe(request) :
    sub = forms.Subscribe()
    if request.method == 'POST':
        sub = forms.Subscribe(request.POST)
        subject = 'Welcome to DataFlair'
        message = 'Hope you are enjoying your Django Tutorials'
        recepient = str(sub['Email'].value())
        send_mail(subject, 
            message, EMAIL_HOST_USER, [recepient], fail_silently = False)
        return render(request, 'subscribe/success.html', {'recepient': recepient})
    return render(request, 'subscribe/index.html', {'form':sub})

edit 2

the templates in /app are shown correctly only the ones in /subscribe have the error does anyone have any ideas?

CodePudding user response:

You need to mention your template dir path in settings.py

Refer this

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],

DIRS defines a list of directories where the engine should look for template source files, in search order.

CodePudding user response:

Try this to organise your templates like this: i will assume that subscribe is your app.

subscribe/templates/subscribe/<put your html files here>
  • Related