janos
janos

Reputation: 124666

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

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.

Upvotes: 44

Views: 16417

Answers (6)

İlkem Tünay
İlkem Tünay

Reputation: 21

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

Upvotes: 2

Yier
Yier

Reputation: 51

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.

Upvotes: 5

lmiguelvargasf
lmiguelvargasf

Reputation: 69795

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.

Upvotes: 3

saturdayplace
saturdayplace

Reputation: 8730

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.

Upvotes: 2

Arman Ordookhani
Arman Ordookhani

Reputation: 6536

Try adding TEMPLATE_LOADERS to your settings file if you don't have it. I think PyCharm looks for it and doesn't load it from Django default settings. It solved my problem.

settings.py:

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

Upvotes: 21

ProfHase85
ProfHase85

Reputation: 12183

Just open the project view (view->tool windows -> project). There right-click on your templates-folder -> 'mark directory as'-> Template directory

Upvotes: 115

Related Questions