0

I'm getting TemplateDoesNotExist at /task/ error.

This is my folder structure for project:

https://i.stack.imgur.com/7zMG2.png

This is my taskmate/urls.py:

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('task/',include('todolist_app.urls')),
    path('todolist/',include('todolist_app.urls')),
]

This is my todolist_app/urls.py:

from django.urls import path
#from todolist_app import views
from . import views

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

This is my todolist_app/views.py:

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

# Create your views here.
def todolist(request):
    return render(request, 'todolist.html',{})

This is my settings.py(important components)

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [(os.path.join(os.path.dirname(os.path.dirname(__file__)),'todolist_app/templates').replace('\\','/'))],#'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'm highly suspicious that the issue I'm getting is due to "DIRS" of template. I have tried couple of different things there, but none seem to have worked.

These are what I've tried there:

    'DIRS': [os.path.join(BASE_DIR,'templates')],
    'DIRS': [os.path.join(os.path.dirname(__file__),'templates').replace('\\','/')],#'DIRS': 
    'DIRS': [(os.path.join(os.path.dirname(os.path.dirname(__file__)),'todolist_app/templates').replace('\\','/'))],#'DIRS': 

I've also added "todolist_app" to installed apps.

thebjorn
  • 26,297
  • 11
  • 96
  • 138
pieszen06
  • 1
  • 2
  • Simply add this `'DIRS': [BASE_DIR / 'templates'],` – Sunderam Dubey Oct 09 '22 at 06:26
  • Can you share the project structure in the site itself by editing the question? Its not clear there. Also can you share the error traceback? – Sunderam Dubey Oct 09 '22 at 06:27
  • 'TypeError: unsupported operand type(s) for /: 'str' and 'str' 'This error comes when I do what Sunderam Dubey said in first comment. – pieszen06 Oct 09 '22 at 06:32
  • https://dpaste.com/B92G6JT8J Heres the traceback with the 'DIRS': [(os.path.join(os.path.dirname(os.path.dirname(_ _file_ _)),'todolist_app/templates').replace('\\','/'))],#'DIRS': – pieszen06 Oct 09 '22 at 06:39
  • You need to give appname if thery is any. – Sunderam Dubey Oct 09 '22 at 06:46
  • Does this answer your question? [Django TemplateDoesNotExist?](https://stackoverflow.com/questions/1926049/django-templatedoesnotexist) – Sunderam Dubey Oct 09 '22 at 06:50
  • @SunderamDubey I've tried all steps given there. But still failing. – pieszen06 Oct 09 '22 at 07:13
  • At last, you should restart server in another port using `python manage.py runserver 500` if then also it not solves the problem, then the last option is you can share GitHub repo, I'll see it. – Sunderam Dubey Oct 09 '22 at 07:52

2 Answers2

0

You need to add app_name to your html. This might solve the issue

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

# Create your views here.
def todolist(request):
    return render(request, 'app_name/todolist.html',{})

Update:

make sure your settings.py TEMPLATE section is configured as below:

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',
            ],
        },
    },
]
SS_SS
  • 63
  • 7
0

What fixed the issue was just add html/todolist.html in render. i.e this in views.py of todolist_app:

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

# Create your views here.
def todolist(request):
    return render(request, 'html/todolist.html',{})

This is not my solution.

pieszen06
  • 1
  • 2