PyCharm code inspection complains template file not found, how to fix?

Viewed 12980

I'm new to PyCharm, and I'm trying to use it for Django development. My app is structured like this:

bs3app/
├── __init__.py
├── templates
│   └── home.html
├── urls.py
└── views.py

In bs3app/views.py, I get a warning:

Template file 'home.html' not found

The source code:

from django.shortcuts import render_to_response


def home(request):
    return render_to_response('home.html')

The template file is there in bs3app/templates/home.html. The home view works correctly when I run the site. On the Django Support page of the project, Enable Django Support is on, and the Django project root, Settings and Manage script values are all correct.

So, why am I getting the warning? As per this PyCharm doc page, I can mark the template directories explicitly and then the warning goes away, but why do I have to? Why can't PyCharm figure them out automatically, given the settings.py file of the project?

The project is on GitHub, if you need to see other files.

6 Answers

If you have marked directory as template directory, but still get the warning, you can try changing the single quotes to double quotes

def home(request):
    return render_to_response('home.html')

change to this:

def home(request):
    return render_to_response("home.html")

Hope this helps.

I am using PyCharm 2018.2. You can follow the next steps to mark a directory as a template directory, so PyCharm will not give you warnings, and you will be able to go to the template by pressing cmd + B (Mac) or ctrl + B (Windows/Linux):

  1. Open the Settings dialog, and click the Project Structure page. You can use cmd + , (on macOS), or by choosing File | Settings (Windows/Linux) enter image description here

  2. Choose the directory to be marked as a template root. enter image description here

  3. Click on Templates on Mark as. In Mac you can also press alt+T. enter image description here

  4. Click on OK to apply the changes you made.

I had a similar issue that was cause by forgetting to include my app in settings.INSTALLED_APPS. Adding my app to the list cleared the inspection.

It's fixed when I assigned "Django project root" from settings to project destination.

Related