Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 563
janos Avatar asked Jan 01 '14 21:01

janos


People also ask

How do I add a template to Pycharm?

Press Ctrl+Alt+S to open the IDE settings and select Editor | File and Code Templates. and specify the template name, file extension, name of the resulting file, and body of the template. Apply the changes and close the dialog.

How do I change the template language in Pycharm?

Configure a template language for a projectOpen the Settings/Preferences dialog, and click the node Python Template Languages. In the Template Languages page, do the following: From the Template language list, select the specific template language to be used in project.

How do you create a template in Python?

The Python string Template is created by passing the template string to its constructor. It supports $-based substitutions. This class has 2 key methods: substitute(mapping, **kwds): This method performs substitutions using a dictionary with a process similar to key-based mapping objects.

How do I add an author name in Pycharm?

TL;DR Goto Settings | File Templates switch to Includes tab, create a new file and type e.g. #set( $MyName = "John Smith" ) . Then go to your template in the Templates tab and add to the template on the top #parse ("your-include-name. your-extension") Now you can use $MyName in this template.


2 Answers

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

like image 148
ProfHase85 Avatar answered Sep 24 '22 14:09

ProfHase85


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', ) 
like image 20
Arman Ordookhani Avatar answered Sep 23 '22 14:09

Arman Ordookhani