1

I am trying to work towards deploying my application and have done most of my work on Windows. However now that I have moved it over to AWS it does not work anymore.

Before anyone links other articles here is a list of all the things I have tried:

  • Adding os.path.join(BASE_DIR,'templates') to TEMPLATE_DIR
  • Changing the permissions of my template
  • My app is in my INSTALLED_APPS variable
  • I have checked that all my render methods are formatted correctly
  • Re-downloaded my project completely

Like I said earlier, it works on my Windows machine, just not on AWS or my Mac. Even more strange is that the error says

Django tried loading these templates, in this order:

Using engine django:
    * django.template.loaders.filesystem.Loader: /opt/bitnami/apps/django/django_projects/goFiles/uploads/templates/uploads/base.html  (Source does not exist)
    * django.template.loaders.app_directories.Loader: /opt/bitnami/apps/django/django_projects/goFiles/uploads/templates/uploads/base.html  (Source does not exist)
    * django.template.loaders.app_directories.Loader: /opt/bitnami/python/lib/python3.8/site-packages/django/contrib/admin/templates/uploads/base.html  (Source does not exist)
    * django.template.loaders.app_directories.Loader: /opt/bitnami/python/lib/python3.8/site-packages/django/contrib/auth/templates/uploads/base.html  (Source does not exist)

And /opt/bitnami/apps/django/django_projects/goFiles/uploads/templates/uploads/base.html is the correct absolute path to the template I am looking for. If I try to cat that file, using that absolute path, it prints to stdout. All this being said I will include some of my code for critical review.

views.py

def get(self, request, *args, **kwargs):
        paste = None
        return render(request, 'uploads/index.html', {'paste': paste})

    def post(self, request, *args, **kwargs):
        paste = request.POST
        if is_post_valid(request):
            return upload(request)
        return render(request, 'uploads/index.html', {'paste': paste})

INSTALLED_APPS

INSTALLED_APPS = [
    'uploads.apps.UploadsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

TEMPLATES

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['/opt/bitnami/apps/django/django_projects/goFiles/uploads/templates/uploads/'],
        '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',
            ],
        },
    },
]

Full Stack Trace

Environment:


Request Method: GET
Request URL: http://184.72.80.110:8000/

Django Version: 3.1
Python Version: 3.8.5
Installed Applications:
['uploads.apps.UploadsConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['goFiles.middleware.upload.UploadCheckMiddleware',
 '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.filesystem.Loader: /opt/bitnami/apps/django/django_projects/goFiles/uploads/templates/uploads/base.html  (Source does not exist)
    * django.template.loaders.app_directories.Loader: /opt/bitnami/apps/django/django_projects/goFiles/uploads/templates/uploads/base.html  (Source does not exist)
    * django.template.loaders.app_directories.Loader: /opt/bitnami/python/lib/python3.8/site-packages/django/contrib/admin/templates/uploads/base.html  (Source does not exist)
    * django.template.loaders.app_directories.Loader: /opt/bitnami/python/lib/python3.8/site-packages/django/contrib/auth/templates/uploads/base.html  (Source does not exist)


Template error:
In template /opt/bitnami/apps/django/django_projects/goFiles/uploads/templates/uploads/index.html, error at line 1
   uploads/base.html 
   1 :  {% extends "uploads/base.html "%} 
   2 : {% load static %}
   3 : {% block content %}
   4 :     <div class="w3-container w3-border w3-border-color-gray" id="paste_container">
   5 :       <!-- Source: https://codepen.io/jacoahmad/pen/vZXrOm -->
   6 :       <div class="o-constrained" id="o-constrained">
   7 :         {% if messages %}
   8 :         {% for message in messages %}
   9 :           <div class="w3-panel w3-red w3-round">
   10 :             <p>{{message}}<p>
   11 :           </div>


Traceback (most recent call last):
  File "/opt/bitnami/python/lib/python3.8/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/opt/bitnami/python/lib/python3.8/site-packages/django/template/base.py", line 170, in render
    return self._render(context)
  File "/opt/bitnami/python/lib/python3.8/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/opt/bitnami/python/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/opt/bitnami/python/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/opt/bitnami/python/lib/python3.8/site-packages/django/template/loader_tags.py", line 127, in render
    compiled_parent = self.get_parent(context)
  File "/opt/bitnami/python/lib/python3.8/site-packages/django/template/loader_tags.py", line 124, in get_parent
    return self.find_template(parent, context)
  File "/opt/bitnami/python/lib/python3.8/site-packages/django/template/loader_tags.py", line 103, in find_template
    template, origin = context.template.engine.find_template(
  File "/opt/bitnami/python/lib/python3.8/site-packages/django/template/engine.py", line 129, in find_template
    raise TemplateDoesNotExist(name, tried=tried)

The above exception (uploads/base.html ) was the direct cause of the following exception:
  File "/opt/bitnami/python/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/opt/bitnami/python/lib/python3.8/site-packages/django/core/handlers/base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/opt/bitnami/python/lib/python3.8/site-packages/django/views/generic/base.py", line 73, in view
    return self.dispatch(request, *args, **kwargs)
  File "/opt/bitnami/python/lib/python3.8/site-packages/django/views/generic/base.py", line 101, in dispatch
    return handler(request, *args, **kwargs)
  File "/opt/bitnami/apps/django/django_projects/goFiles/uploads/views.py", line 18, in get
    return render(request, 'uploads/index.html', {'paste': paste})
  File "/opt/bitnami/python/lib/python3.8/site-packages/django/shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/opt/bitnami/python/lib/python3.8/site-packages/django/template/loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "/opt/bitnami/python/lib/python3.8/site-packages/django/template/backends/django.py", line 63, in render
    reraise(exc, self.backend)
  File "/opt/bitnami/python/lib/python3.8/site-packages/django/template/backends/django.py", line 84, in reraise
    raise new from exc

Exception Type: TemplateDoesNotExist at /
Exception Value: uploads/base.html 

If I need to include any more information I am happy to do so. Any help would be much appreciated.

  • 1
    Set `'DIRS': ['/opt/bitnami/apps/django/django_projects/goFiles/uploads/templates/'],` – JPG Sep 13 '20 at 04:30

2 Answers2

1

you have put extra space at the end of extends i manually tried that django won't allow any extra spaces

from:
    {% extends "uploads/base.html "%} 
                                 ^
to:
    {% extends "uploads/base.html" %} 
                                 ^
Kishan Parmar
  • 819
  • 6
  • 8
  • 1
    Can confirm this is the answer. It is wild how long you can look at the same error and not notice the one space that is making you life hell. – Stefan Tobler Sep 13 '20 at 04:45
  • @kishan parmar i have issue with when i file template/appname/index.html i the i move file in template dire root it work how to fixed ? – Mr Coder Jul 17 '21 at 10:53
0

##django template root directory you can use sub directory template files

settings.py

make sure the "BASE_DIR" defined before use

Build paths inside the project like this: BASE_DIR / 'subdir'.

BASE_DIR = Path(__file__).resolve().parent.parent

update 'DIRS': [BASE_DIR ,'templates'],

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

create template directory into your project root folder.

create sub directory inside the template folders. "blogs" is my sub directory

view.py

def post_list(request):
    posts = Post.published.all()
    return render(request,'blogs/post_list.html',{'posts':posts})

Mr Coder
  • 507
  • 3
  • 13