0

I have encountered an issue with my Django app and despite looking at similar posts on this topic, I haven't been able to find a solution that works for me.

I'm currently working on my first Django app, a project called "Learning log" based on the "Python Crash Course" book. I followed the instructions in the book, but when I try to access my app on Heroku, I receive a TemplateDoesNotExists error. However, everything seems to work fine when I run the app locally.

Error message:

TemplateDoesNotExist at /users/register/
registration/register.html
Request Method: GET
Request URL:    https://guarded-citadel-16510.herokuapp.com/users/register/
Django Version: 4.1.7
Exception Type: TemplateDoesNotExist
Exception Value:    
registration/register.html
Exception Location: /app/.heroku/python/lib/python3.11/site-packages/django/template/loader.py, line 19, in get_template
Raised during:  users.views.register
Python Executable:  /app/.heroku/python/bin/python
Python Version: 3.11.2
Python Path:    
['/app/.heroku/python/bin',
'/app',
'/app/.heroku/python/lib/python311.zip',
'/app/.heroku/python/lib/python3.11',
'/app/.heroku/python/lib/python3.11/lib-dynload',
'/app/.heroku/python/lib/python3.11/site-packages']
Server time:    Fri, 31 Mar 2023 17:37:57 +0000
Template-loader postmortem
Django tried loading these templates, in this order:

Using engine django:

django.template.loaders.app_directories.Loader: /app/learning_logs/templates/registration/register.html (Source does not exist)
django.template.loaders.app_directories.Loader: /app/users/templates/registration/register.html (Source does not exist)
django.template.loaders.app_directories.Loader: /app/.heroku/python/lib/python3.11/site-packages/bootstrap4/templates/registration/register.html (Source does not exist)
django.template.loaders.app_directories.Loader: /app/.heroku/python/lib/python3.11/site-packages/django/contrib/admin/templates/registration/register.html (Source does not exist)
django.template.loaders.app_directories.Loader: /app/.heroku/python/lib/python3.11/site-packages/django/contrib/auth/templates/registration/register.html (Source does not exist)

Even though I have stored my template in the path "/app/users/templates/registration/register.html" and Django attempted to access it from that location, I received an error message stating that the source does not exist.

settings.py:

"""Django settings for learning_log project.

Generated by 'django-admin startproject' using Django 4.1.7.

For more information on this file, see
https://docs.djangoproject.com/en/4.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.1/ref/settings/
"""
from pathlib import Path
import os

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-s2e&@i+^l71(q333kfya@_$h1sl-7l&e76o%rsfd_tf27z5&7u"

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    "learning_logs",
    "users",
    "bootstrap4",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
]

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",
]

ROOT_URLCONF = "learning_log.urls"

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",
            ],
        },
    },
]

WSGI_APPLICATION = "learning_log.wsgi.application"


# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": BASE_DIR / "db.sqlite3",
    }
}


# Password validation
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
    },
    {"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",},
    {"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",},
    {"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",},
]


# Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/

STATIC_ROOT = BASE_DIR / "staticfiles"
STATIC_URL = "static/"

# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

LOGIN_URL = "users:login"

import django_heroku

django_heroku.settings(locals())

heroku logs:

 ยป   Warning: heroku update available from 7.53.0 to 8.0.2.
2023-03-31T17:37:21.483911+00:00 app[web.1]: certfile: None
2023-03-31T17:37:21.483911+00:00 app[web.1]: ssl_version: 2
2023-03-31T17:37:21.483911+00:00 app[web.1]: cert_reqs: 0
2023-03-31T17:37:21.483911+00:00 app[web.1]: ca_certs: None
2023-03-31T17:37:21.483911+00:00 app[web.1]: suppress_ragged_eofs: True
2023-03-31T17:37:21.483912+00:00 app[web.1]: do_handshake_on_connect: False
2023-03-31T17:37:21.483912+00:00 app[web.1]: ciphers: None
2023-03-31T17:37:21.483912+00:00 app[web.1]: raw_paste_global_conf: []
2023-03-31T17:37:21.483912+00:00 app[web.1]: strip_header_spaces: False
2023-03-31T17:37:21.483977+00:00 app[web.1]: [2023-03-31 17:37:21 +0000] [2] [INFO] Starting gunicorn 20.1.0
2023-03-31T17:37:21.484335+00:00 app[web.1]: [2023-03-31 17:37:21 +0000] [2] [DEBUG] Arbiter booted
2023-03-31T17:37:21.484370+00:00 app[web.1]: [2023-03-31 17:37:21 +0000] [2] [INFO] Listening at: http://0.0.0.0:43364 (2)
2023-03-31T17:37:21.484422+00:00 app[web.1]: [2023-03-31 17:37:21 +0000] [2] [INFO] Using worker: sync
2023-03-31T17:37:21.487416+00:00 app[web.1]: [2023-03-31 17:37:21 +0000] [7] [INFO] Booting worker with pid: 7
2023-03-31T17:37:21.549569+00:00 app[web.1]: [2023-03-31 17:37:21 +0000] [8] [INFO] Booting worker with pid: 8
2023-03-31T17:37:21.572512+00:00 app[web.1]: [2023-03-31 17:37:21 +0000] [2] [DEBUG] 2 workers
2023-03-31T17:37:22.112525+00:00 app[web.1]: [2023-03-31 17:37:22 +0000] [7] [DEBUG] Closing connection.
2023-03-31T17:37:22.124565+00:00 app[web.1]: [2023-03-31 17:37:22 +0000] [7] [DEBUG] Closing connection.
2023-03-31T17:37:22.136137+00:00 heroku[web.1]: State changed from starting to up
2023-03-31T17:37:48.684490+00:00 app[web.1]: [2023-03-31 17:37:48 +0000] [7] [DEBUG] GET /
2023-03-31T17:37:48.737820+00:00 heroku[router]: at=info method=GET path="/" host=guarded-citadel-16510.herokuapp.com request_id=1588a061-8ce6-47d7-a811-bf2a3578cdeb fwd="31.183.200.87" dyno=web.1 connect=0ms service=55ms status=200 bytes=2642 protocol=https
2023-03-31T17:37:48.738951+00:00 app[web.1]: 10.1.89.104 - - [31/Mar/2023:17:37:48 +0000] "GET / HTTP/1.1" 200 2346 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
2023-03-31T17:37:50.993908+00:00 app[web.1]: [2023-03-31 17:37:50 +0000] [7] [DEBUG] GET /users/register/
2023-03-31T17:37:51.018368+00:00 heroku[router]: at=info method=GET path="/users/register/" host=guarded-citadel-16510.herokuapp.com request_id=54199276-c77f-4f70-97cf-f9871bdce494 fwd="31.183.200.87" dyno=web.1 connect=0ms service=26ms status=500 bytes=72381 protocol=https
2023-03-31T17:37:51.018637+00:00 app[web.1]: Internal Server Error: /users/register/
2023-03-31T17:37:51.018638+00:00 app[web.1]: Traceback (most recent call last):
2023-03-31T17:37:51.018643+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/django/core/handlers/exception.py", line 56, in inner
2023-03-31T17:37:51.018643+00:00 app[web.1]: response = get_response(request)
2023-03-31T17:37:51.018644+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^
2023-03-31T17:37:51.018645+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
2023-03-31T17:37:51.018653+00:00 app[web.1]: response = wrapped_callback(request, *callback_args, **callback_kwargs)
2023-03-31T17:37:51.018654+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-03-31T17:37:51.018654+00:00 app[web.1]: File "/app/users/views.py", line 17, in register
2023-03-31T17:37:51.018655+00:00 app[web.1]: return render(request, "registration/register.html", context)
2023-03-31T17:37:51.018655+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-03-31T17:37:51.018655+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/django/shortcuts.py", line 24, in render
2023-03-31T17:37:51.018656+00:00 app[web.1]: content = loader.render_to_string(template_name, context, request, using=using)
2023-03-31T17:37:51.018656+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-03-31T17:37:51.018656+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/django/template/loader.py", line 61, in render_to_string
2023-03-31T17:37:51.018657+00:00 app[web.1]: template = get_template(template_name, using=using)
2023-03-31T17:37:51.018657+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-03-31T17:37:51.018657+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/django/template/loader.py", line 19, in get_template
2023-03-31T17:37:51.018657+00:00 app[web.1]: raise TemplateDoesNotExist(template_name, chain=chain)
2023-03-31T17:37:51.018658+00:00 app[web.1]: django.template.exceptions.TemplateDoesNotExist: registration/register.html
2023-03-31T17:37:51.019178+00:00 app[web.1]: 10.1.89.104 - - [31/Mar/2023:17:37:51 +0000] "GET /users/register/ HTTP/1.1" 500 72065 "https://guarded-citadel-16510.herokuapp.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
2023-03-31T17:37:53.396916+00:00 heroku[router]: at=info method=GET path="/topics/" host=guarded-citadel-16510.herokuapp.com request_id=083b7221-18e0-41dd-b04c-3d8a94e08e0b fwd="31.183.200.87" dyno=web.1 connect=0ms service=1ms status=302 bytes=335 protocol=https
2023-03-31T17:37:53.396920+00:00 app[web.1]: [2023-03-31 17:37:53 +0000] [7] [DEBUG] GET /topics/
2023-03-31T17:37:53.398125+00:00 app[web.1]: 10.1.89.104 - - [31/Mar/2023:17:37:53 +0000] "GET /topics/ HTTP/1.1" 302 0 "https://guarded-citadel-16510.herokuapp.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
2023-03-31T17:37:53.528165+00:00 app[web.1]: [2023-03-31 17:37:53 +0000] [7] [DEBUG] GET /users/login/
2023-03-31T17:37:53.547541+00:00 heroku[router]: at=info method=GET path="/users/login/?next=/topics/" host=guarded-citadel-16510.herokuapp.com request_id=25f18fce-285f-40d7-b2c6-1c63108fa303 fwd="31.183.200.87" dyno=web.1 connect=0ms service=21ms status=500 bytes=72375 protocol=https
2023-03-31T17:37:53.547887+00:00 app[web.1]: Internal Server Error: /users/login/
2023-03-31T17:37:53.547888+00:00 app[web.1]: Traceback (most recent call last):
2023-03-31T17:37:53.547889+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/django/core/handlers/exception.py", line 56, in inner
2023-03-31T17:37:53.547889+00:00 app[web.1]: response = get_response(request)
2023-03-31T17:37:53.547890+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^
2023-03-31T17:37:53.547890+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/django/core/handlers/base.py", line 220, in _get_response
2023-03-31T17:37:53.547890+00:00 app[web.1]: response = response.render()
2023-03-31T17:37:53.547891+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^
2023-03-31T17:37:53.547891+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/django/template/response.py", line 114, in render
2023-03-31T17:37:53.547891+00:00 app[web.1]: self.content = self.rendered_content
2023-03-31T17:37:53.547891+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^
2023-03-31T17:37:53.547892+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/django/template/response.py", line 90, in rendered_content
2023-03-31T17:37:53.547892+00:00 app[web.1]: template = self.resolve_template(self.template_name)
2023-03-31T17:37:53.547892+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-03-31T17:37:53.547893+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/django/template/response.py", line 72, in resolve_template
2023-03-31T17:37:53.547893+00:00 app[web.1]: return select_template(template, using=self.using)
2023-03-31T17:37:53.547893+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-03-31T17:37:53.547893+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/django/template/loader.py", line 47, in select_template
2023-03-31T17:37:53.547894+00:00 app[web.1]: raise TemplateDoesNotExist(", ".join(template_name_list), chain=chain)
2023-03-31T17:37:53.547894+00:00 app[web.1]: django.template.exceptions.TemplateDoesNotExist: registration/login.html
2023-03-31T17:37:53.548354+00:00 app[web.1]: 10.1.89.104 - - [31/Mar/2023:17:37:53 +0000] "GET /users/login/?next=/topics/ HTTP/1.1" 500 72059 "https://guarded-citadel-16510.herokuapp.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
2023-03-31T17:37:57.352906+00:00 app[web.1]: [2023-03-31 17:37:57 +0000] [7] [DEBUG] GET /users/register/
2023-03-31T17:37:57.372591+00:00 heroku[router]: at=info method=GET path="/users/register/" host=guarded-citadel-16510.herokuapp.com request_id=47fea280-b3a1-42dd-9c45-52b1975ec07f fwd="31.183.200.87" dyno=web.1 connect=0ms service=21ms status=500 bytes=72381 protocol=https
2023-03-31T17:37:57.372883+00:00 app[web.1]: Internal Server Error: /users/register/
2023-03-31T17:37:57.372886+00:00 app[web.1]: Traceback (most recent call last):
2023-03-31T17:37:57.372886+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/django/core/handlers/exception.py", line 56, in inner
2023-03-31T17:37:57.372887+00:00 app[web.1]: response = get_response(request)
2023-03-31T17:37:57.372887+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^
2023-03-31T17:37:57.372888+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
2023-03-31T17:37:57.372888+00:00 app[web.1]: response = wrapped_callback(request, *callback_args, **callback_kwargs)
2023-03-31T17:37:57.372888+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-03-31T17:37:57.372889+00:00 app[web.1]: File "/app/users/views.py", line 17, in register
2023-03-31T17:37:57.372899+00:00 app[web.1]: return render(request, "registration/register.html", context)
2023-03-31T17:37:57.372899+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-03-31T17:37:57.372900+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/django/shortcuts.py", line 24, in render
2023-03-31T17:37:57.372900+00:00 app[web.1]: content = loader.render_to_string(template_name, context, request, using=using)
2023-03-31T17:37:57.372901+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-03-31T17:37:57.372901+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/django/template/loader.py", line 61, in render_to_string
2023-03-31T17:37:57.372901+00:00 app[web.1]: template = get_template(template_name, using=using)
2023-03-31T17:37:57.372901+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-03-31T17:37:57.372902+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/django/template/loader.py", line 19, in get_template
2023-03-31T17:37:57.372902+00:00 app[web.1]: raise TemplateDoesNotExist(template_name, chain=chain)
2023-03-31T17:37:57.372902+00:00 app[web.1]: django.template.exceptions.TemplateDoesNotExist: registration/register.html
2023-03-31T17:37:57.373421+00:00 app[web.1]: 10.1.89.104 - - [31/Mar/2023:17:37:57 +0000] "GET /users/register/ HTTP/1.1" 500 72065 "https://guarded-citadel-16510.herokuapp.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
2023-03-31T17:39:01.271837+00:00 app[web.1]: [2023-03-31 17:39:01 +0000] [8] [DEBUG] Closing connection.
2023-03-31T17:41:13.059689+00:00 app[web.1]: [2023-03-31 17:41:13 +0000] [8] [DEBUG] Closing connection.
2023-03-31T17:43:25.179335+00:00 app[web.1]: [2023-03-31 17:43:25 +0000] [8] [DEBUG] Closing connection.
2023-03-31T17:45:36.949129+00:00 app[web.1]: [2023-03-31 17:45:36 +0000] [8] [DEBUG] Closing connection.
2023-03-31T17:47:49.098156+00:00 app[web.1]: [2023-03-31 17:47:49 +0000] [8] [DEBUG] Closing connection.
2023-03-31T17:50:01.397810+00:00 app[web.1]: [2023-03-31 17:50:01 +0000] [7] [DEBUG] Closing connection.

And finally my project directory.

Any suggestions on what might be causing this issue and how to resolve it would be greatly appreciated. Thank you in advance!

Ravdar
  • 23
  • 6
  • Hi, I'm the author of PCC, so I know this project pretty well. :) Everything you've shown looks like it's correct. It's possible the template was not pushed to Heroku. Are you working on a main/master branch locally? If not, you may have pushed a different branch than what you're using locally. Can you push your project to GitHub? It's easier to troubleshoot if we can poke around the whole project. โ€“ japhyr Mar 31 '23 at 19:36
  • Hi! Excellent book, sir. I am pretty sure that I've pushed the same branch that is currently working on my local machine. Here's the link to the project repository on GitHub: https://github.com/Ravdar/learning_log. โ€“ Ravdar Apr 04 '23 at 16:15
  • Check out your [users app here](https://github.com/Ravdar/learning_log/tree/master/users). Note that you can't click on the `templates/` directory. I think this [SO discussion](https://stackoverflow.com/questions/62056294/github-folders-have-a-white-arrow-on-them) is relevant. Maybe you were in the templates directory when you first ran `git init`? โ€“ japhyr Apr 04 '23 at 20:17

0 Answers0