Search code examples
htmldjangoauthenticationtemplatesheroku

How to solve Django TemplateDoesNotExist at accounts/login.html error?


I made a blog app in Django and deployed it with Heroku. When I opened the app, get 'TemplateDoesNotExist at /accounts/login/' error. It should load the login.html and it did not load it. If anyone could help me, I would be thankful.

I tried 'DIRS': [os.path.join(BASE_DIR, 'Templates')], but did not work. Or maybe I did something wrong.

Here is my file structure:

BLOG
├─.idea
├─accounts
|    ├─__pycache__
|    ├─migrations
|    ├─Templates
|    |  └─accounts
|    |     ├─login.html
|    |     └─signup.html
|    ├─__init__.py
|    ├─admin.py
|    ├─apps.py
|    ├─models.py
|    ├─tests.py
|    ├─urls.py
|    └─views.py
├─articles
|    ├─__pycache__
|    ├─migrations
|    ├─Templates
|    |  └─articles
|    |     ├─article_create.html
|    |     ├─article_details.html
|    |     ├─article_form.html
|    |     ├─article_list.html
|    |     └─pagination.html
|    ├─__init__.py
|    ├─admin.py
|    ├─apps.py
|    ├─forms.py
|    ├─models.py
|    ├─tests.py
|    ├─urls.py
|    └─views.py
├─blog
|    ├─__pycache__
|    ├─Templates
|    |  └─default.html
|    ├─__init__.py
|    ├─asgi.py
|    ├─settings.py
|    ├─urls.py
|    ├─views.py
|    └─wsgi.py
├─media
├─static
|  ├─favicon.ico
|  ├─logo.png
|  ├─slugify.js
|  ├─styles.css
|  └─background.jpg
├─db.sqlite3
└─manage.py

Here is some part of my settings.py:

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

Solution

  • Based on the folder structure you have shown here, you need to add in this format in your TEMPLATES - DIRS key

    'DIRS': [os.path.join(BASE_DIR, 'accounts', 'Templates', 'accounts'),
             os.path.join(BASE_DIR, 'articles', 'Templates', 'articles'),
             os.path.join(BASE_DIR, 'blogs', 'Templates')]
    

    This should fix the issue.