Why am I getting a TemplateDoesNotExist error in my Django project?

Hey everyone, I’m running into a weird issue with my Django project. I keep getting a TemplateDoesNotExist error for my course_list.html template. It’s super confusing because my browser shows Django 1.11.1, but PyCharm says I’m using 2.0.3.

Here’s my course_list.html template:

{% for course in courses %}
<h3>{{ course.name }}</h3>
<p>{{ course.summary }}</p>
{% endfor %}

And my urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.list_courses),
]

My views.py looks like this:

from django.shortcuts import render
from .models import Subject

def list_courses(request):
    subjects = Subject.objects.all()
    return render(request, 'subjects/list.html', {'subjects': subjects})

I’ve double-checked my file paths and everything seems correct. Any ideas what might be causing this error? Thanks in advance for your help!

hey mate, i’ve run into this before. ur trying to render ‘subjects/list.html’ but ur template is ‘course_list.html’. that’s probably why it’s throwing that error.

try changing ur view to render ‘course_list.html’ instead. also make sure ur template is in the right folder, usually it’s in ur_app/templates/ur_app/.

hope this helps!

I’ve encountered this exact issue before, and it can be frustrating. From your code, I noticed you’re trying to render ‘subjects/list.html’, but your template is named ‘course_list.html’. This mismatch is likely causing the TemplateDoesNotExist error.

To fix this, either rename your template to ‘subjects/list.html’ or update your view to render ‘course_list.html’. Also, ensure your template is in the correct directory - typically it should be in your_app/templates/your_app/.

Regarding the Django version discrepancy, I’d recommend checking your virtual environment. Sometimes IDEs like PyCharm use a different environment than your terminal. Run ‘pip freeze | grep Django’ in your project’s environment to confirm the actual version you’re using.

Lastly, double-check your TEMPLATES setting in settings.py to ensure it’s correctly pointing to your template directories. These steps should resolve your issue.